change activesheet in a for loop

djamieson17

New Member
Joined
Nov 13, 2011
Messages
5
Hi All,

I have an array that contains a list of worksheet names. I want to change the activesheet in a loop.

psuedo-code:

array = ("sheet1" "sheet2" "sheet3")

for i = 1 to length of array
change active sheet to array(i)
... rest of macro ...
next j

I've tried a number of (what I thought were) obvious ways, but cannot get it to work.

Thanks,

Dave
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Dave

Why do you want to change the active sheet?

Does the rest of the macro rely on the sheet being active?

Why not reference the sheet instead of activing it?

Code:
arrShts = Array("Sheet1", "Sheet2", "Sheet3")
 
For I = LBound(arrShts) To UBound(arrShts)
 
      Set ws = Worksheets(arrShts(i))
       ' rest of code
Next I

Even if all you want to do is activate the sheet you can still use this code and simply add ws.Activate.
 
Upvote 0
Norie,

I am trying to (for each sheet in my array) unhide all cells, then hide all rows where a 0 exists in column a. my code so far is:


'asSht is my array
lngth = Application.CountA(asSht)

For j = 1 To lngth
Set wks = Worksheets(asSht(j)) ''i want to change the activesheet here!
ActiveSheet.Rows.EntireRow.Hidden = False
For Each c In ActiveSheet.Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If c = "0" Then
ActiveSheet.Rows(c.Row).Hidden = True
End If
Next c
Next j

I am just struggling to change the activesheet. If you can help me, or think of a more efficient way of doing this, I would be very grateful.

Thanks,

Dave
 
Upvote 0
Dave

Change Activesheet to wks, then you don't need to activate the sheet.
 
Upvote 0
Guys,

Thanks very much. Works fine now. You were right enough, there was no need to activate each sheet when I could just reference them.

Thanks again,

Dave
 
Upvote 0

Forum statistics

Threads
1,215,408
Messages
6,124,727
Members
449,185
Latest member
ekrause77

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