目标:
我的程序目前计算一个月使用的加热和冷却成本值。现在,我试图让用户进入多个月的使用时间。
我做得太远了
我创建了函数(以及Userform和输出表),用于计算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
如您所见,这些功能只接受单个月。 userform具有多个月的选项,选中后会显示复选框,每月一个。
单击模拟按钮时,将执行以下代码:
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不会让我上传图片,所以我不能告诉你userform。
问题:
我无法找到最有效的方法来获得所选择的多个月的总费用。我有一些想法在蹦蹦跳跳但似乎无法解决它们。每当我开始头脑风暴时,我最终会得到大量的代码或大量独立的功能或者对于任务而言似乎很荒谬的东西。我想我可以使用现有的功能,但是当他们选择多个月时我不知道怎么做。
提前感谢您的帮助。如果有任何不清楚的地方,请告诉我。这对我来说很有意义,但对其他人来说可能是一团糟。
答案 0 :(得分:0)
如果我理解了正确的内容就足以在不创建多个选择的新公式的情况下就足够了:
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