使用VBA(以及一般编码)相对较新,但我不能理解为什么这不起作用。
我希望根据我的Userform中的组合框中选择的两个选项来输出不同的输出。
为什么上帝为什么"如果oindex
= 1,无论index
是什么值,都会生成。
Private Sub SubmitButton_Click()
Dim oindex As Integer
oindex = Output.ListIndex
If (index = 0 And oindex = 1) Then
Range("A7").Value = "WHY GOD WHY"
End If
Unload UserForm
End Sub
Private Sub UserForm_Initialize()
With Demand
.AddItem "I want policy details"
.AddItem "I would like a value"
.AddItem "I want to cancel my policy"
.AddItem "I want to change my address"
.AddItem "I would like Surrender Forms"
.AddItem "I would like to update my bank details"
.AddItem "I want to make an alteration on my policy"
.AddItem "I want to transfer my plan"
.AddItem "I have a fund query"
End With
End Sub
Private Sub Demand_Change()
Dim index As Integer
index = Demand.ListIndex
Output.Clear
Select Case index
Case Is >= 0
With Output
.AddItem "I need to provide this information verbally"
.AddItem "I need to update/send this myself"
.AddItem "I need to ask back-office to update/send this"
End With
End Select
End Sub
希望你能帮忙。
答案 0 :(得分:4)
范围。
Private Sub Demand_Change()
Dim index As Integer
此处index
位于Demand_Change()
的本地,SubmitButton_Click()
中访问该private index as integer
之外的{{1}}将不会提供该值,但会被视为零。
而是在模块顶部将其声明为{{1}}。
答案 1 :(得分:3)
由于您没有显示Option Explicit
,并且您没有显示宣布index
的位置,我会假设在此子例程中,index
是即时创建:
Private Sub SubmitButton_Click()
Dim oindex As Integer
oindex = Output.ListIndex
If (index = 0 And oindex = 1) Then
Range("A7").Value = "WHY GOD WHY"
End If
Unload UserForm
End Sub
由于永远不会为index
分配值,因此它保持默认值0。
答案 2 :(得分:3)
或者,直接使用您的对象。我不认为有必要使用模块范围的变量:
Private Sub SubmitButton_Click()
If (Me.Demand.ListIndex = 0 And Me.Output.ListIndex = 1) Then
Range("A7").Value = "WHY GOD WHY"
End If
Unload UserForm
End Sub
原因是您已经在表单控件本身中处理了那些值。使用模块或公共范围的变量在技术上工作,但是可能变得更难以管理和跟踪错误或调试,并且它也是多余的。
如果您正在使用这些值,多次在SubmitButton_Click
子例程中并希望使用变量以简化,那么请在过程级别执行此操作:
Private Sub SubmitButton_Click()
Dim dIndex as Integer, oIndex as Integer
dIndex = Me.Demand.ListIndex
oIndex = Me.Output.ListIndex
If (dIndex = 0 And oIndex = 1) Then
Range("A7").Value = "WHY GOD WHY"
End If
Unload UserForm
End Sub