我正在工作并且遇到了一些障碍。对于一个我不是最强大的程序员,我刚刚接受了这个任务,但遇到了障碍(我认为它的语法但不确定)。
我们已经做了大量的研究,为一些技术不太好的顾问找到了一些数据相关性,这个VBA工具将成为他们为客户提供实时预测的快速参考。该主题与该问题无关。
我创建了一个包含值的类,然后计划是从VBA用户表单引用该类。当我引用课程时,我没有得到我存储的值,但我得到了该属性的名称。
Private Sub ddMonthOfUse_Change()
Dim mv As String
Set chillWater = New clsMonth
With chillWater
.Janurary = 0.7136
.Feburary = 0.6755
.March = 0.6528
.April = 0.7773
.May = 0.8213
.June = 0.8715
.July = 0.9
.August = 1.0243
.September = 1.0516
.October = 0.8514
.November = 0.7095
.December = 0.6994
End With
Set DX = New clsMonth
With DX
.Janurary = 0.5777
.Feburary = 0.5536
.March = 0.5166
.April = 0.6112
.May = 0.7035
.June = 0.75
.July = 0.8
.August = 0.8345
.September = 0.9333
.October = 0.6865
.November = 0.5976
.December = 0.4907
End With
MsgBox chillWater.month
End Sub
在课堂模块中我有这个
Option Explicit
'property decleration
Public Janurary As Single
Public Feburary As Single
Public March As Single
Public April As Single
Public May As Single
Public June As Single
Public July As Single
Public August As Single
Public September As Single
Public October As Single
Public November As Single
Public December As Single
Public chillWater As clsMonth, DX As clsMonth
Public Property Get month() As String
month = Main.ddMonthOfUse.value
End Property
如果用户选择月份3月,而不是消息框返回值,则消息框显示" March。"例如,如果我替换下拉列表的引用并只输入March,我会得到值。
我无法解决这个问题。请记住,我根本不是一名经验丰富的程序员,我对这个主题有非常基本的知识,所以我可以完全离开这里。另外我应该提一下,消息框不是信息的最终用途,我只是在测试我是否正确地调用它。
答案 0 :(得分:1)
@Mat' sMug当选择了userform中的组合框时(比如用户选择了May),我希望消息框显示chillwater的值。可能
好的,明白了。除去所有这些,它们有毒:
Public chillWater As clsMonth, DX As clsMonth
Public Property Get month() As String
month = Main.ddMonthOfUse.value
End Property
您需要公开一个需要一个月名称的Function
并返回相应字段的值。
一种方法是利用CallByName
并执行以下操作:
Public Function ValueFor(ByVal monthName As String) As Single
On Error GoTo CleanFail
Dim instance As Object
Set instance = Me 'CallByName can't take "Me" directly
Dim result As Single
result = CallByName(instance, monthName, VbGet)
CleanExit:
ValueFor = result
Exit Function
CleanFail:
result = 0
Resume CleanExit
End Function
请注意VbGet
参数 - 它将使用公共字段(我刚测试过),但理想情况下,您将封装这些值并将其公开为< em> properties 。我喜欢封装我的东西:
Option Explicit
Private Type TMonth '"T" + ClassName, just by convention
January As Single
February As Single
'...
December As Single
End Type
Private this As TMonth 'the only private field you need!
'now expose a property accessor for each member:
Public Property Get January() As Single
January = this.January
End Property
Public Property Let January(ByVal value As Single)
this.January = value
End Property
'...
然后您的表单代码可以执行此操作:
Set chillWater = New clsMonth
With chillWater
.Janurary = 0.7136
.Feburary = 0.6755
.March = 0.6528
.April = 0.7773
.May = 0.8213
.June = 0.8715
.July = 0.9
.August = 1.0243
.September = 1.0516
.October = 0.8514
.November = 0.7095
.December = 0.6994
MsgBox "Value for " & ddMonthOfUse.value & ": " & .ValueFor(ddMonthOfUse.Value)
End With