Select multiple sheets with VBA
Tessel75
-
Tessel75 -
Tessel75 -
Bonjour,
To select multiple sheets in VBA without naming them all, you can use the following expression: ```vba Sheets(n & ":" & m).Select ``` Where `n` and `m` are the indices of the first and last sheets you want to select.
For copying and pasting, you can express it like this: ```vba Array(Sheets(n), Sheets(n + 1), ..., Sheets(m)) ``` However, VBA does not support array slicing in the same way as some other languages. Consequently, you would have to define each sheet individually unless you write a loop to construct the array.
I hope this helps!
Configuration: Windows 7 / Chrome 24.0.1312.52
To select multiple sheets in VBA without naming them all, you can use the following expression: ```vba Sheets(n & ":" & m).Select ``` Where `n` and `m` are the indices of the first and last sheets you want to select.
For copying and pasting, you can express it like this: ```vba Array(Sheets(n), Sheets(n + 1), ..., Sheets(m)) ``` However, VBA does not support array slicing in the same way as some other languages. Consequently, you would have to define each sheet individually unless you write a loop to construct the array.
I hope this helps!
Configuration: Windows 7 / Chrome 24.0.1312.52
4 answers
Hello,
To select multiple sheets based on their index, you need to use an array, for example:
--
Best regards
Patrice
To select multiple sheets based on their index, you need to use an array, for example:
Sub Test() Dim tblIndex() As Integer 'Array of 2 values ReDim tblIndex(1) 'Definition of the 2 sheets tblIndex(0) = 2 tblIndex(1) = 4 'Add a sheet ReDim Preserve tblIndex(2) tblIndex(2) = 5 Worksheets(tblIndex).Select End Sub
--
Best regards
Patrice