Array Loop Help

nnicolazzi

New Member
Joined
Jun 9, 2017
Messages
5
Greetings! Long time reader, first time poster. I'm sure there is an answer to this question somewhere but I have spent hours reading trying to find a solution and still cannot find it. Any help will be greatly appreciated! Thanks!

Goal: Use a For Loop to loop through an Array of Sheet Names and do some method.

Problem: The problem that I am having is that when I run the macro it only updates the sheet that is already selected in the workbook. It also says that my variable is "Empty" even after I set the variable to equal the array.

Example: For illustration I've simplified my project to create and array of specific sheets in my workbook and set the value of cell A1 = 1.

The code is below.


Sub Macro2()


Dim MyArray As Variant
Dim i As Variant


Set MyArray = Sheets(Array("Room 1", "Room 2", "Room 3"))


For Each i In MyArray


ActiveSheet.Cells(1, 1) = 1


Next i


End Sub

Thanks so much again! any help/articles/links/ will be greatly appreciated
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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