Im trying to automate a data management, I want to be able to select a variable number of sheets on an excel workbook, starting from a known sheet number, then loop into each sheet to select the data an then copy it into another sheet in the workbook so I can have a consolidate of all the data sheets and create a pivot table from it. For example the Sheets would look like this: PivotTable,ConsolidatedData,Sheet1,Sheet2,Sheet3,....SheetN
So I want to take the data from each Sheet into the Consolidated data sheet. What Im trying to do is to create an iterable dynamic object made of selected sheets so then I can iterate over every sheet and copy its data.
Can this be done in VBA?
This is what i have tried:
'Selecting worksheets'
Dim x As Integer
ThisWorkbook.Worksheets(7).Select
For x = 8 To ThisWorkbook.Worksheets.Count
Worksheets(x).Select (False)
Next x
Dim MyArray As Variant
MyArray = Array(ActiveWindow.SelectedSheets)
'Loopin through selection of worksheets'
Dim ws As Worksheet
For Each ws In MyArray
ws.Activate
'Copy/paste of data'
Range("U9").Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("ConsolidatedData").Activate
Range("B1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Next ws
I´ve tried doing it without using the "MyArray" object and using ActiveWindow.SelectedSheets but this does not allows me to go back and forth from the ConsolidatedData to the Sheets that have the data. What I want to do is possible or should I do it differently?
My main doubt is if this is even possible in some way:
Dim MyArray As Variant
MyArray = Array(ActiveWindow.SelectedSheets)