VBA:如何将内容从多选框(在用户窗体中创建)传递到模块

VBA: How to pass the contents from multi-checkbox (created in userform) to module

我想将多选框(在用户窗体中创建)中的内容传递给模块以供进一步使用。 这个函数在代码中是:

在模块中:

 Public pass1 As Boolean
 Public pass2 As Boolean
 Public pass3 As Boolean
 Public pass4 As Boolean
Public Sub MultiCheckBoxes()
    Dim varArraySelected As Variant
    Dim ivar As Long
    varArraySelected = Array()

    UserForm1.Show

    ivar = 0
    If pass1 = True Then
        ReDim Preserve varArraySelected(0 To ivar)
        varArraySelected(ivar) = "Fase 1"
        ivar = ivar + 1 'Advance the counter to next array item

    End If
    If pass2 = True Then
            ReDim Preserve varArraySelected(0 To ivar) 'Reset the array dimension on each iteration of loop
            varArraySelected(ivar) = "Fase 2" 'Add value in Column B to Array
            ivar = ivar + 1 'Advance the counter to next array item

    End If
    If pass3 = True Then
            ReDim Preserve varArraySelected(0 To ivar) 'Reset the array dimension on each iteration of loop
            varArraySelected(ivar) = "Fase 3" 'Add value in Column B to Array
            ivar = ivar + 1 'Advance the counter to next array item

    End If
    If pass4 = True Then
            ReDim Preserve varArraySelected(0 To ivar) 'Reset the array dimension on each iteration of loop
            varArraySelected(ivar) = "Fase 4"  'Add value in Column B to Array

    End If
    Unload UserForm1

End Sub

在用户表单中:

Option Explicit    
Private Sub CheckBox1_Click()    
End Sub
Private Sub CheckBox2_Click()      
End Sub
Private Sub CheckBox3_Click()  
End Sub
Private Sub CheckBox4_Click()    
End Sub

Private Sub CommandButton1_Click()        
    pass1 = UserForm1.CheckBox1.Value
    pass2 = UserForm1.CheckBox2.Value    
    pass3 = UserForm1.CheckBox3.Value  
    pass4 = UserForm1.CheckBox4.Value
    Unload UserForm1
End Sub

这里'Fase 1',...,'Fase 4'是UserForm1中创建的四个checkbox的文本内容

当 运行 VBA 模块时,出现错误 'Ambiguous name detected: pass1'。如何解决问题?

提前致谢!

将您的子更改为:

Public Sub MultiCheckBoxes(pass1 As Boolean, pass2 As Boolean, pass3 As Boolean,pass4 As Boolean)

然后这样称呼它:

MultiCheckBoxes(UserForm1.CheckBox1.Value, UserForm1.CheckBox2.Value, UserForm1.CheckBox3.Value, UserForm1.CheckBox4.Value)