“和”function不检查是否满足两个标准

使用VBA(和一般编码)相对较新,但我不明白为什么这是行不通的。

我想要一个不同的输出发生在我的用户窗体combobox中select了两个选项。

如果oindex = 1,无论什么价值index是什么, oindex产生“为什么为什么”世代。

 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 

希望你能帮上忙。

范围。

 Private Sub Demand_Change() Dim index As Integer 

这里的indexDemand_Change()本地index ,如果在SubmitButton_Click()SubmitButton_Click()将不提供该值,但将被视为零。

相反,在模块的顶部将其声明为private index as integer

既然你没有显示一个Option Explicit ,而且你没有显示index被声明在哪里,我会假设在这个子例程中, index正在被dynamic创build:

 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。

或者,直接使用您的对象。 我不认为有任何需要使用模块范围的variables:

 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 

原因是你已经有了这些的处理,在窗体控件本身。 使用模块或公共范围的variables在技术上是可行的 ,但是可能变得更难pipe理和跟踪错误或debugging,而且也是多余的。

如果您正在处理这些值,请在SubmitButton_Click子例程中多次使用这些值,并希望为了简洁而使用variables ,然后在过程级别执行此操作:

 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