我是VBA的新手,并且正在尝试通过自学来完成这个项目。我正在尝试使用几个月的ComboBox下拉菜单和一个文本框来输入年份的用户表单。然后我想将这些值保存为变量并传回主模块。我引用了Assigning a variable directly from a userform和Passing variable from Form to Module in VBA,但他们所做的并不适合我。 This is what my form looks like。
我的表单名为Dates。组合框是Correct_Month;文本框是Correct_Year。我想将这些值保存在变量Month和Year中。
这就是我对用户形式所拥有的:
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub Correct_Month_Change()
Month = Correct_Month.Value
End Sub
Private Sub Correct_Year_Change()
Year = Correct_Year.Value
End Sub
Private Sub OkButton_Click()
'Message box forces a response
If Correct_Month.Text = "" Or Correct_Year.Value = "" Then
MsgBox ("You must enter a month and year to continue")
Exit Sub
End If
Unload Me
End Sub
Private Sub UserForm_Initialize()
Correct_Month.Clear
Correct_Month.List = Array("January", "February", "March", "April", "May", _
"June", "July", "August", "September", "October", "November", _
"December")
Correct_Year.Value = ""
End Sub
这是我的模块:
Private Sub Monthly_Report_Test()
'~~> more coding
Dates.Show
'something here to call variables over?
driver.findElementByName("Dates").SendKeys Month
driver.findElementByName("Dates").SendKeys Year
Correct_Info = MsgBox("Is the following information correct?" & vbCrLf & vbCrLf & _
"Report Month: " & Month & vbCrLf & vbCrLf & "Report Year: " & Year, _
vbYesNoCancel, "Month & Year")
If Correct_Info = vbCancel Then
Exit Sub
End If
'~~> more coding
End Sub
我尝试了几种不同的东西,这就像我能得到的一样。谁能告诉我我需要做什么以及为什么?非常感谢你!
答案 0 :(得分:0)
在vba中,您可以从userform获取control(=您的组合框)的值 只需编写 NameOfUserForm.NameOfControl
即可在您的情况下 - 添加到您的模块行:
Dim Var1, Var2
Var1 = Dates.Correct_Month.Value
Var2 = Dates.Correct_Year.Value
现在你可以使用变量:Var1& Var2 - 你想要的。