Sheet Name as Array Value

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
this code is from @Fluff VBA Run macros on multiple sheets, not all sheets
VBA Code:
Sub Button1_Click()
   Dim ws As Worksheet  
   '** SET The Sheet Names - MUST Reflect Each Sheet Name Exactly!
   WkSheets = Array("ATC1", "BEC1", "DKC1")  
   For Each ws In Sheets(Array("ATC1", "BEC1", "DKC1"))
      ws.Select
      Call CheckGoalSeekRev 'Macro1
   Next ws
End Sub
I used code below to retrieve and store my sheetname to array
VBA Code:
sCount = 1
For Each rngCell In Range("A3:A" & Cells(Rows.Count, "C").End(xlUp).Row)
    If rngCell.Value <> "" Then
        arrValue(sCount) = rngCell.MergeArea(1).Value
        sCount = sCount + 1
    End If
Next

is there a way to used my stored array sheetname in the code above?
or is there a different way to utilized my code for the "For Loop" code?

My concept code

FROM

VBA Code:
    WkSheets = Array("ATC1", "BEC1", "DKC1")  
    For Each ws In Sheets(Array("ATC1", "BEC1", "DKC1"))
TO
VBA Code:
    wsLoop = arrValue()
    For Each ws In Sheets(arrValue())
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
VBA Code:
 s = ""
     For Each rngCell In Range("A3:A" & Cells(Rows.Count, "C").End(xlUp).Row)
          If rngCell.Value <> "" Then s = s & "|" & rngCell.MergeArea(1).Value     'collect all those values with a "|" as separator
     Next
     sp = Split(Mid(s, 2), "|")     'split on that "|" and ignore the 1st

     For Each ws In Sheets(sp)
 
Upvote 0
VBA Code:
 s = ""
     For Each rngCell In Range("A3:A" & Cells(Rows.Count, "C").End(xlUp).Row)
          If rngCell.Value <> "" Then s = s & "|" & rngCell.MergeArea(1).Value     'collect all those values with a "|" as separator
     Next
     sp = Split(Mid(s, 2), "|")     'split on that "|" and ignore the 1st

     For Each ws In Sheets(sp)
rt error 9 - subscript out of range
VBA Code:
Dim s As String
Dim ws As Worksheet
Dim sp As Variant
 s = ""
     For Each rngCell In Range("A3:A" & Cells(Rows.Count, "C").End(xlUp).Row)
          If rngCell.Value <> "" Then s = s & "|" & rngCell.MergeArea(1).Value     'collect all those values with a "|" as separator
     Next
     sp = Split(Mid(s, 2), "|")     'split on that "|" and ignore the 1st

     For Each ws In Sheets(sp)
    
     Next
 
Upvote 0
VBA Code:
For each ws in sheets
    For i = 1 to ubound(arrValue)
       If ws.name like arrValue(i) then
            ---put the code here
       end if
    Next
Next
 
Upvote 0
Solution
Hi,
updating your approach a little maybe this will do what you want

VBA Code:
Dim arrValue() As Variant

Sub DisplayArrayElements()
    Dim ws As Worksheet
  
    For Each ws In Sheets(arrValue)
        MsgBox ws.Name
    Next ws

End Sub


Sub LoadArray()
    Dim sCount  As Long
    Dim rngCell As Range
  
    For Each rngCell In Range("A3:A" & Cells(Rows.Count, "C").End(xlUp).Row)
        If rngCell.Value <> "" Then
            sCount = sCount + 1
            ReDim Preserve arrValue(1 To sCount)
            arrValue(sCount) = rngCell.MergeArea(1).Value
        End If
    Next
  
End Sub

code is just an example idea & does not check if sheet exists or if array has been intilalized

Dave
 
Upvote 0
Hi,
updating your approach a little maybe this will do what you want

VBA Code:
Dim arrValue() As Variant

Sub DisplayArrayElements()
    Dim ws As Worksheet
 
    For Each ws In Sheets(arrValue)
        MsgBox ws.Name
    Next ws

End Sub


Sub LoadArray()
    Dim sCount  As Long
    Dim rngCell As Range
 
    For Each rngCell In Range("A3:A" & Cells(Rows.Count, "C").End(xlUp).Row)
        If rngCell.Value <> "" Then
            sCount = sCount + 1
            ReDim Preserve arrValue(1 To sCount)
            arrValue(sCount) = rngCell.MergeArea(1).Value
        End If
    Next
 
End Sub

code is just an example idea & does not check if sheet exists or if array has been intilalized

Dave
works well, I hope I can marked twice :)
marked @bebo021999 for the solution since he first replied! but thank you anyway
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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