the main goal is copying the worksheets name to an array, and array passes from function to main code to be able to track new added worksheets name to the array and work with them accordingly
Public Function CountWorksheets() As String
Dim i, size As Integer
Dim Arr() As String
size = Worksheets.Count
ReDim Arr(size)
For i = 1 To Worksheets.Count
Arr(i) = Worksheets(i).Name
Next i
CountWorksheets = Arr
End Function
Private Sub RunLoc_Click()
Dim ar() As String
ReDim ar(Worksheets.Count)
j = 1
If Not IsEmpty(Location.Value) Then
RunLoc.Enabled = True
For i = 1 To Worksheets.Count
If Worksheets(i).Name = Location.Value Then
Worksheets(i).Select
Range("a1").Select
End If
Next i
ar = CountWorksheets()
End If
TextBox1.Value = Location.Value
End Sub
I expect to have the Arr and CountWorksheets with the same array recorded in both in function, and when CountWorksheets is called, the total of the array is copied to the new one which is named ar.