Array and range logic

YounesB3

Board Regular
Joined
Mar 28, 2012
Messages
148
Hi, I've always wondered why this kind of codes doesn't work:

Code:
Worksheets("Titles").Rows("1:1").Copy Worksheets(Array("V1", "V2").Rows(1)).Insert Shift:=xlDown

Any help is appreciated.
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Worksheets("V1") will return a Worksheet object. However, Worksheets(Array("V1", "V2")) returns a Worksheets collections object.

Worksheet object allows you to access and update its properties as well as access its member objects, like Range.

Worksheets collection allows you to perform sheet level operations since it is a collection of worksheets. That is kind of like any Collections object, like a list or set or queue. As with collection objects, you have to first access the object in it, then access its member methods/properties.
 
Upvote 0
Worksheets("V1") will return a Worksheet object. However, Worksheets(Array("V1", "V2")) returns a Worksheets collections object.

Worksheet object allows you to access and update its properties as well as access its member objects, like Range.

Worksheets collection allows you to perform sheet level operations since it is a collection of worksheets. That is kind of like any Collections object, like a list or set or queue. As with collection objects, you have to first access the object in it, then access its member methods/properties.

Thanks for the reply! Would you have an example?

I know this would work and would probably be the simplest way since it's only two sheets in this case:

Code:
Worksheets("Titles").Rows("1:1").Copy Worksheets("V1").Rows(1).Insert Shift:=xlDown
Worksheets("Titles").Rows("1:1").Copy Worksheets("V2").Rows(1).Insert Shift:=xlDown

But what would it look like with an array?
 
Upvote 0
You need to use a loop.

Code:
Private Sub ShiftRowsInSheets(aSheetNames As Variant)
    Dim sheetName As Variant
    Dim oSheet As Worksheet
    
    For Each sheetName In aSheetNames
        Worksheets("Titles").Rows("1:1").Copy
        Worksheets(sheetName).Rows(1).Insert Shift:=xlDown
        Application.CutCopyMode = False
    Next
End Sub



Private Sub TestSub()
    ShiftRowsInSheets Array("Sheet1", "Sheet2")
End Sub
 
Upvote 0
You need to use a loop.

Code:
Private Sub ShiftRowsInSheets(aSheetNames As Variant)
    Dim sheetName As Variant
    Dim oSheet As Worksheet
    
    For Each sheetName In aSheetNames
        Worksheets("Titles").Rows("1:1").Copy
        Worksheets(sheetName).Rows(1).Insert Shift:=xlDown
        Application.CutCopyMode = False
    Next
End Sub



Private Sub TestSub()
    ShiftRowsInSheets Array("Sheet1", "Sheet2")
End Sub

Oh I see, I thought there would be a more efficient way. Thanks! :)
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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