Trouble with Public Dynamic Array

eyesbee

New Member
Joined
Jan 23, 2013
Messages
2
Hi,

I've been looking online for the last couple of hours for a solution to my problem. I've created a sub that works if I declare the array within the sub (see commented out declaration line below). However, I want to use the array in another module. So I've added in the Public line at the top of the code (see below).

' Declare the array that will hold information about the specific queries to run
Public arrCheckedBoxes() As String

Code:
Public arrCheckedBoxes() As String

Public Sub CheckBox_Run_Click()

Dim intChecked As Integer
'Dim arrCheckedBoxes() As String

ThisWorkbook.Worksheets("Control Panel").Select

' Count the number of "Run" boxes that are checked
intChecked = 0
For Each chkbox In ActiveSheet.CheckBoxes
    If Left(chkbox.Name, 3) = "Run" Then
        If chkbox.Value = xlOn Then
            intChecked = intChecked + 1
            ReDim Preserve arrCheckedBoxes(intChecked)
            arrCheckedBoxes(intChecked) = chkbox.Name
        End If
    End If
Next chkbox
'MsgBox (intChecked)

End Sub

I get a compile error saying "Variable not defined". The following code is highlighted when the error message box pops up: "ReDim Preserve arrCheckedBoxes" -- full line of code is "ReDim Preserve arrCheckedBoxes(intChecked)".

I can't really pass the array on to the other sub because the other sub runs off a click event. All I'm aiming to do at the moment is read the list of elements of arrCheckedBoxes() in another module's subroutine.

Thanks in advance :).
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I had a feeling that the 'Option Explicit' statement specified in another module may be causing this error. I worked around it by adding the ReDim statement into the sub as follows:

Code:
Public Sub CheckBox_Run_Click()Dim intChecked As Integer
'Dim arrCheckedBoxes() As String
ReDim arrCheckedBoxes(0) As String
' The rest of the sub remains intact ...

Case closed unless anybody cares to explain? I'm still confused. The helpfile on 'Explicit' says, if used at module level, it should only impact the module it appears in.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top