Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default VBA: Activate sheets within a loop

I want to loop through a list of sheets, why won't the following code allow
me to activate a sheet? Thanks for your help in advance!

Sub LoopThroughSheets()

Dim wSheet As Worksheet

i = 5
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Do While wSheet < ""

Sheets(wSheet).Activate
Calculate

i = i + 1
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Loop

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA: Activate sheets within a loop

Hi,

There's no need to activate the sheet to calculate, try this

Sub LoopThroughSheets()
Dim wSheet As String
i = 5
wSheet = Sheets("Print Tracks").Range("G" & i).Value
Do While wSheet < ""
Sheets(wSheet).Calculate
i = i + 1
wSheet = Sheets("Print Tracks").Range("G" & i).Value
Loop
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"CM4@FL" wrote:

I want to loop through a list of sheets, why won't the following code allow
me to activate a sheet? Thanks for your help in advance!

Sub LoopThroughSheets()

Dim wSheet As Worksheet

i = 5
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Do While wSheet < ""

Sheets(wSheet).Activate
Calculate

i = i + 1
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Loop

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default VBA: Activate sheets within a loop

You have wSheet declared as a worksheet. I assume it needs to be declared as
a String, because it looks like you are looping through a list of sheet
names. This is how you should declare it

Dim wSheet As String

I actually recommend you looping through your sheets like this instead.
You don't have to activate the sheet to calculate it. Hope this helps! If
so, let me know, click "YES" below.

Sub LoopThroughSheets()

Dim i As Long

i = 5
Do While Sheets("Print Tracks").Cells(i, "G").Value < ""
Sheets(Cells(i, "G").Value).Calculate
i = i + 1
Loop

End Sub

--
Cheers,
Ryan


"CM4@FL" wrote:

I want to loop through a list of sheets, why won't the following code allow
me to activate a sheet? Thanks for your help in advance!

Sub LoopThroughSheets()

Dim wSheet As Worksheet

i = 5
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Do While wSheet < ""

Sheets(wSheet).Activate
Calculate

i = i + 1
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Loop

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default VBA: Activate sheets within a loop

It's actually better to loop through objects (in your case the object is
cells) with the For Each...Loop instead of For...Loop. Try this code. If
your list of sheet names in Col. G is continues without any blanks in between
sheet names this code would work a little better. Hope this helps! If so,
let me know, click "YES" below.

Sub LoopThroughSheets()

Dim LastRow As Long
Dim MyRange As Range
Dim cell As Range

With Sheets("Print Tracks")
' find last row in column G
LastRow = .Cells(Rows.Count, "G").End(xlUp).Row
' set range to scan with loop
Set MyRange = .Range("G5:G" & LastRow)
End With

' scan each cell in range of sheet names
For Each cell In MyRange
Sheets(cell.Value).Calculate
Next cell

End Sub
--
Cheers,
Ryan


"CM4@FL" wrote:

I want to loop through a list of sheets, why won't the following code allow
me to activate a sheet? Thanks for your help in advance!

Sub LoopThroughSheets()

Dim wSheet As Worksheet

i = 5
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Do While wSheet < ""

Sheets(wSheet).Activate
Calculate

i = i + 1
wSheet = Sheets("Print Tracks").Range("G" & i).Value

Loop

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop to Filter, Name Sheets. If Blank, Exit Loop ryguy7272 Excel Programming 3 February 5th 08 03:41 PM
visual basic - select/activate but don't show sheets Donna Edwards Excel Programming 1 November 11th 07 05:44 PM
Activate Sheet Best Practice (Y/N?) when xFer data between sheets? [email protected] Excel Programming 2 July 3rd 07 04:52 PM
Activate all sheets George Gee New Users to Excel 10 December 3rd 05 10:00 PM
Any difference between Sheets Activate and Select? Don Wiss Excel Programming 1 October 28th 03 12:46 AM


All times are GMT +1. The time now is 07:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"