Selecting multiple worksheets

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Hi there folks,
I know how to press ctrl and select multiple sheets so as to apply something to all selected sheets. But I'm trying to do this in VBA

My following code is supposed to copy the faux header (whatever the user selects, typically the first few rows) which often contains images and copy this to all other visible sheets within the workbook. I know my issue is that my red line would normally have sheets(array("Sheet2","Sheet3")).select and so on but I'm trying to do this dynamically so the quotes aren't actually in my array. It is sheets(array(Sheet2,Sheet3)).select This doesn't work. Please help. Thanks

Code:
Sub copyfauxheader()
Dim sourcesh As Worksheet
Dim wks As Worksheet
Dim wksarraystr As String
Dim wksarray() As String
Selection.Copy
Set sourcesh = ActiveSheet
For Each wks In ThisWorkbook.Sheets
If sourcesh.Name <> wks.Name And wks.Visible = True Then
If wksarraystr = "" Then
wksarraystr = wks.Name
Else
wksarraystr = wksarraystr & "," & wks.Name
End If
wks.Select
wks.Rows(1).Select
    End If
    Next
wksarray = Split(wksarraystr, ",")
[COLOR=#ff0000] Sheets(Array(wksarray)).Select[/COLOR]
 Selection.Insert Shift:=xlDown
    sourcesh.Select
    Range("A7").Select
    Application.CutCopyMode = False
    MsgBox "Done"
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This worked. Stored the selected address, then when I looped back through the array of target sheets, I kept re-selecting the source range, copy

Code:
Sub copyfauxheader()
Dim sourcesh As Worksheet
Dim wks As Worksheet
Dim wksarraystr As String
Dim wksarray() As String
Dim wksstr As String
Dim seladd As String
Set sourcesh = ActiveSheet
seladd = Selection.Address
For Each wks In ThisWorkbook.Sheets
If sourcesh.Name <> wks.Name And wks.Visible = True Then
If wksarraystr = "" Then
wksarraystr = wks.Name
Else
wksarraystr = wksarraystr & "," & wks.Name
End If
    End If
    Next
    wksarray = Split(wksarraystr, ",")
For x = LBound(wksarray) To UBound(wksarray)
wksstr = CStr(wksarray(x))
sourcesh.Range(seladd).Copy
Sheets(wksstr).Select
Sheets(wksstr).Rows(1).Select
Selection.Insert Shift:=xlDown
 Next x
    sourcesh.Select
    Range("A1").Select
    Application.CutCopyMode = False
    MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,027
Messages
6,128,381
Members
449,445
Latest member
JJFabEngineering

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