Search code examples
excelvbacheckboxparameter-passinguserform

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


I'd like to pass the contents from multicheckbox (created in userform) to the module for further use. This function in the code is:

In the 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

In the Userform:

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

Here 'Fase 1',..., 'Fase 4' are the text contents in the four checkboxes created in UserForm1.

When running the VBA module, I got errors 'Ambiguous name detected: pass1'. How to solve the problem?

Thanks in advance!


Solution

  • Change your sub to this:

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

    Then call it like this:

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