循环使用多个参数获取值
Posted
技术标签:
【中文标题】循环使用多个参数获取值【英文标题】:Loop to get value using multiple parameters 【发布时间】:2016-07-07 16:11:36 【问题描述】:目标:
我的程序目前计算一个月的供暖和制冷成本值。现在,我试图让用户输入多个月的使用情况。
到目前为止我做了什么:
我创建了计算 2 种不同情况(总共 4 个函数)的加热和冷却成本的函数(连同用户表单和输出表)。下面是其中之一的代码。不同功能之间的唯一变化是旁边有 2 颗星的线。这些是 VBA 用户表单中的 4 个单独的下拉菜单(可能有更有效的方法来做到这一点,但可惜我们在这里)。
Function CoolingCostS1(month As String, sqft As Single, ElecAUC As Single, Days As Single) As Single
'variable declaeration
Dim cost As Single
Select Case Main.ddRegion.value 'Case statement for region value
Case "South"
**Select Case Main.ddS1Cooling.value**
Case "Chill Water System"
Set chillWater = New clsMonth 'create new object for CWS system
With chillWater 'assign calculated values
.January = 0.7136 / 20
.February = 0.6755 / 20
.March = 0.6528 / 20
.April = 0.7773 / 20
.May = 0.8213 / 20
.June = 0.8715 / 20
.July = 0.9 / 20
.August = 1.0243 / 20
.September = 1.0516 / 20
.October = 0.8514 / 20
.November = 0.7095 / 20
.December = 0.6994 / 20
cost = .ValueFor(month) * sqft * ElecAUC * Days
End With
Case "Direct Expansion"
Set DX = New clsMonth
With DX
.January = 0.577 / 20
.February = 0.553 / 20
.March = 0.516 / 20
.April = 0.611 / 20
.May = 0.703 / 20
.June = 0.74 / 20
.July = 0.801 / 20
.August = 0.834 / 20
.September = 0.9333 / 20
.October = 0.686 / 20
.November = 0.597 / 20
.December = 0.4907 / 20
cost = .ValueFor(month) * sqft * ElecAUC * Days
End With
End Select
Case "North"
Select Case Main.ddS1Cooling.value
Case "Chill Water System"
Set chillWater = New clsMonth 'create new object for CWS system
With chillWater 'assign calculated values
.January = 0.775 / 20
.February = 0.845 / 20
.March = 0.699 / 20
.April = 0.722 / 20
.May = 0.751 / 20
.June = 0.1 / 20
.July = 0.9 / 20
.August = 0.95 / 20
.September = 0.946 / 20
.October = 0.749 / 20
.November = 0.739 / 20
.December = 0.75 / 20
cost = .ValueFor(month) * sqft * ElecAUC * Days
End With
Case "Direct Expansion"
Set DX = New clsMonth
With DX
.January = 0.536 / 20
.February = 0.52 / 20
.March = 0.49 / 20
.April = 0.482 / 20
.May = 0.511 / 20
.June = 0.5 / 20
.July = 0.5 / 20
.August = 0.461 / 20
.September = 543 / 20
.October = 0.521 / 20
.November = 0.497 / 20
.December = 0.531 / 20
cost = .ValueFor(month) * sqft * ElecAUC * Days
End With
End Select
End Select
CoolingCostS1 = cost
End Function
如您所见,这些函数只接受单个月份。用户表单有一个多月选项,选中后会显示复选框,每个月一个。
当点击模拟按钮时,执行以下代码:
Private Sub bSimulate_Click() 'logic to calculate simulation values
'''''''''''variable decleration'''''''''''''''''''''''''
Dim s1Sqft As Single, s2Sqft As Single, s1ElecAUC As Single, s2ElecAUC As Single, s1HeatAUC As Single, s2HeatAUC As Single, Days As Single
'''''''''''conditional if and logic'''''''''''''''''''''
If IsNumeric(Me.txtS2elec.value) = True And IsNumeric(Me.txtS2NG.value) = True And IsNumeric(Me.txtS2sqft.value) = True And Me.ddS2cooling.ListIndex > -1 And Me.ddS2Heating.ListIndex > -1 Then
**case for unselected using prior information**
**case for selected using new information**
'variable assignment
s1Sqft = txtS1sqft.value 'system 1
s1ElecAUC = txtS1elec.value
s1HeatAUC = Me.txtS1NG.value
s2Sqft = txtS2sqft.value 'system 2
s2ElecAUC = txtS2elec.value
s2HeatAUC = txtS2NG.value
Days = txtUseDays.value 'usage page
'resets cells back to white
Me.txtS2elec.BackColor = vbWhite
Me.txtS2NG.BackColor = vbWhite
Me.txtS2sqft.BackColor = vbWhite
'System one output
Cells(13, 3).value = Days
Cells(14, 3).value = Application.WorksheetFunction.RoundUp(CoolingCostS1(Me.ddMonthOfUse.value, s1Sqft, s1ElecAUC, Days) / s1ElecAUC, 0) & " KWH"
Cells(14, 5).value = Application.WorksheetFunction.RoundUp(CoolingCostS1(Me.ddMonthOfUse.value, s1Sqft, s1ElecAUC, Days), 0)
Cells(15, 3).value = Application.WorksheetFunction.RoundUp((HeatingCostS1(Me.ddMonthOfUse.value, s1Sqft, s1HeatAUC, Days) / s1HeatAUC), 0) & " " & Me.ddHeatingUtility.value
Cells(15, 5).value = Application.WorksheetFunction.RoundUp(HeatingCostS1(Me.ddMonthOfUse.value, s1Sqft, s1HeatAUC, Days), 0)
'System two output
Cells(21, 3).value = Days
Cells(22, 3).value = Application.WorksheetFunction.RoundUp(CoolingCostS2(Me.ddMonthOfUse.value, s2Sqft, s2ElecAUC, Days) / s2ElecAUC, 0) & " KWH"
Cells(22, 5).value = Application.WorksheetFunction.RoundUp(CoolingCostS2(Me.ddMonthOfUse.value, s2Sqft, s2ElecAUC, Days), 0)
Cells(23, 3).value = Application.WorksheetFunction.RoundUp((HeatingCostS2(Me.ddMonthOfUse.value, s2Sqft, s2HeatAUC, Days) / s2HeatAUC), 0) & " " & Me.ddHeatingUtility.value
Cells(23, 5).value = Application.WorksheetFunction.RoundUp(HeatingCostS2(Me.ddMonthOfUse.value, s2Sqft, s2HeatAUC, Days), 0)
'System one information output
Cells(12, 9).value = Me.ddS1Cooling.value
Cells(13, 9).value = Me.ddS1Heating.value
Cells(14, 9).value = Me.txtS1sqft.value
'System two information output
Cells(20, 9).value = Me.ddS2cooling.value
Cells(21, 9).value = Me.ddS2Heating.value
Cells(22, 9).value = Me.txtS2sqft.value
Else
HighlightBadCells2 'checks for incorrect cell input values
MsgBox "Please check the highlighted cells"
GoTo CleanFail
End If
Main.Hide
CleanFail:
End Sub
这两颗星在哪里,那是我集思广益,是否有可能做两个案例——一个选择了几个月,一个没有。
Stack 不允许我上传图片,所以我无法向您展示用户表单。
问题:
我无法找出最有效的方法来获取所选多个月的总成本。我有一些想法在蹦蹦跳跳,但似乎无法将它们表达出来。每次我开始头脑风暴时,我都会得到大量代码或大量单独的函数,或者对于这项任务来说似乎很荒谬。我想我可以使用现有的功能,但是当他们选择多个月份时,我不知道该怎么做。
提前感谢您的帮助。如果有任何不清楚的地方,请告诉我。这对我来说很有意义,但对其他人来说可能是一团糟。
【问题讨论】:
【参考方案1】:如果我理解正确,以下内容就足够了,而无需为多项选择创建新公式:
Cells(22, 3).value = 0
Cells(23, 3).value = 0
For Each month in Me.ddMonthOfUse
Cells(22, 3).value = Cells(22, 3).value + Application.WorksheetFunction.RoundUp(CoolingCostS1(month.value, s1Sqft, s1ElecAUC, Days) / s1ElecAUC, 0) & " KWH"
Cells(23, 3).value = Cells(23, 3).value + Application.WorksheetFunction.RoundUp((HeatingCostS1(month.value, s2Sqft, s2HeatAUC, Days) / s2HeatAUC), 0) & " " & Me.ddHeatingUtility.value
Next month
【讨论】:
有没有办法将其应用于用户表单中选定的月份?如果我理解正确,这将在数据类型中每个月执行一次。 ddMonthOfUse 是一个列表框吗?如果是这样,我将更新以下答案:***.com/a/2933240/3218398以上是关于循环使用多个参数获取值的主要内容,如果未能解决你的问题,请参考以下文章