View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Selecting Next Worksheet

Your code is fine for looping through the sheets. You just need to add a
command as below:

So assuming your procedures work on the activesheet:

Sub Timesheet_Format()

Dim sht As Worksheet

For Each sht In ThisWorkbook.Worksheets
sh.Activate
ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub2.TimeSub2"
Application.Wait Now + TimeValue("00:00:01") 'Delay

ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub3.TimeSub3"
Application.Wait Now + TimeValue("00:00:01") 'Delay

ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub4.TimeSub4"
ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub5.TimeSub5"
ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub6.TimeSub6"
Next

End Sub

I would suggest that you don't name your procedures with names that
duplicate names of modules. I have heard that can be problematic. Also,
you can have more than one procedure in a module.


--
Regards,
Tom Ogilvy

"BSII" wrote in message
...
Thanks everyone - my apologies for not being more specific. What I'm
really
trying to do is write a subroutine tha that will run a couple of
subroutines
on each worksheet in my workbook. The current code is:

Sub Timesheet_Format()

Dim sht As Worksheet

For Each sht In ThisWorkbook.Worksheets

ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub2.TimeSub2"
Application.Wait Now + TimeValue("00:00:01") 'Delay

ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub3.TimeSub3"
Application.Wait Now + TimeValue("00:00:01") 'Delay

ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub4.TimeSub4"
ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub5.TimeSub5"
ThisWorkbook.Application.Run "PERSONAL.XLS!TimeSub6.TimeSub6"
Next

End Sub

The "TimeSubs" are subroutines that format and arrange my data. From the
other suggestions, it sounds like I'm not using the "For Each" correctly.
Is
there a better way to do this?


"Gary Keramidas" wrote:

post your code. maybe you're not qualifying the ranges.

for example:

this will not work:, it will only enter test into the active sheet:
Sub Timesheet_Format()
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
Range("A1") = "test"
Next
End Sub

this will:

Sub Timesheet_Format()
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
sht.Range("A1") = "test"
Next
End Sub
--


Gary


"BSII" wrote in message
...
I'm looking for some code to perform the same group of operations on
each
worksheet in a workbook. It seems to be a very simple operation, but I
can't
seem to get it to work. I have tried the following:

Sub Timesheet_Format()

Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets

' ACTIONS TO BE DONE

Next

End Sub

It runs ok for the current worksheet, but doesn't go on to any of the
others. Any suggestions on what I'm doing wrong?

Thanks...