I'm new to vba and I'm trying to create a subroutine that will perform the same copy and paste operation to 5 workbooks.
I'm trying to do this by building an array containing all of the windows that the function needs to be carried out on as per the below:
Sub Copyinforevised()
Dim i As Integer
Dim z As Integer
i = 1
z = 5
Dim wb(i To z) As Window
wb (1): Set wb1 = Windows("Chariot OPS project workbook.xlsx")
wb (2): Set wb2 = Windows("Chariot RAN project workbook.xlsx")
wb (3): Set wb3 = Windows("Chariot AT project workbook.xlsx")
wb (4): Set wb4 = Windows("Chariot OSS project workbook.xlsx")
wb (5): Set wb5 = Windows("Chariot MOB project workbook.xlsx")
For i = 1 To z
Windows(wb(i)).Activate
[function to be done to the workbook]
Next i
End Sub
But when I try to run the macro I get the error "compile error constant expression queried"
Workbooksinstead ofWindows. Alsowb1etc is not part of the array. So something likeSet wb(1) = Application.Workbooks("Chariot OPS project workbook.xlsx")makes more sense. In your loop you can also then usewb(i).Activatewithout usingWindows.Activate. You don't need to do that as you already have the object. There are many examples on the net which will go into details why we should never use.Activateor.Select. Please have a read