Global/Public Array

joannevanderwest

New Member
Joined
Jul 12, 2019
Messages
3
Hi

I am struggling to declare and fill in a global/public array.
Here is my code



Public Function myArray() As Variant
Dim WS_Count As Integer
Dim i As Integer
Dim x As Integer
Let x = 0
WS_Count = ActiveWorkbook.Worksheets.Count
For i = 1 To WS_Count
If Left(ActiveWorkbook.Worksheets(i).Name, 5) = "Sheep" Then

ReDim Preserve myArray(x) As Variant
myArray(x) = Worksheets(i).Name
x = x + 1
End If
Next i
End Function

I am getting Redim errors

I have a userform with a button. Everytime I click the button I want to go to the next array and then save information to the tab. ( The array is the tabname)

I am a newbie

Thank you
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try creating a temporary array in which to store the desired sheet names, and then transfer the contents to myArray...

Code:
Public Function myArray() As Variant

    Dim tempArray() As Variant
    Dim WS_Count As Integer
    Dim i As Long
    Dim x As Long
    
    WS_Count = ActiveWorkbook.Worksheets.Count
    
    ReDim tempArray(1 To WS_Count)
    
    x = 0
    For i = LBound(tempArray) To UBound(tempArray)
        If Left(ActiveWorkbook.Worksheets(i).Name, 5) = "Sheep" Then
            x = x + 1
            tempArray(x) = ActiveWorkbook.Worksheets(i).Name
        End If
    Next i
    
    ReDim Preserve tempArray(1 To x)
    
    myArray = tempArray
    
End Function

Then you can get your array of sheet names something like this...

Code:
Sub test()

    Dim x As Variant
    Dim i As Long
    
    x = myArray()
    
    For i = LBound(x) To UBound(x)
        Debug.Print x(i)
    Next i
    
End Sub

Hope this helps!
 
Upvote 0
Here is another way to write the myArray function...
Code:
Public Function myArray() As Variant
  Dim WS As Worksheet, Combined As String
  For Each WS In Worksheets
    If Left(WS.Name, 5) = "Sheep" Then Combined = Combined & "/" & WS.Name
  Next
  myArray = Split(Mid(Combined, 2), "/")
End Function
You can use Domenic's subroutine to test it
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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