0

I know similar questions have been asked before like this and this but I was having issues with initializing the checkbox array object (My VBA is quite rusty).

I have the following code:

Dim chkAdQ(4) As Checkbox

Set chkAdQ(0) = chkAdQ1
Set chkAdQ(1) = chkAdQ2
Set chkAdQ(2) = chkAdQ3
Set chkAdQ(3) = chkAdQ4

where chkAdQ1, chkAdQ2 etc. are ActiveX checkboxes present on the form. On debugging I can see that chkAdQ(4) prompts 'nothing' on the declaration itself and hence the assignment gives a Type mismatch exception.

I also tried by declaring chkAdQ(4) as an Object but to no avail. Any thoughts?

1
  • You have not set chkAdQ(4) , so that is nothing. Commented Aug 25, 2017 at 11:49

2 Answers 2

1

You can add all checkboxes on the worksheet quite nicely with a simple loop

Sub AddCheckBoxesToArray()
    Dim chkAdQ As Variant
    Dim cb

    i = 0
    ReDim chkAdQ(i)
    For Each cb In Sheet2.OLEObjects
        If TypeName(cb.Object) = "CheckBox" Then
            If i > 0 Then ReDim Preserve chkAdQ(0 To i)
            Set chkAdQ(i) = cb
            i = i + 1
        End If
    Next cb

    For Each cb In chkAdQ
        Debug.Print cb.Name
    Next cb
End Sub

Remove the second loop when using. This is just to prove that they have all been added by printing their names to the Immediate window

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

Dim chkAdQ(0 To 3) As Variant

Set chkAdQ(0) = chkAdQ1
Set chkAdQ(1) = chkAdQ2
Set chkAdQ(2) = chkAdQ3
Set chkAdQ(3) = chkAdQ4

1 Comment

of all the things I had to try Variant was the one I missed . Thanks !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.