Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Selecting Next Worksheet

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...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Selecting Next Worksheet

Maybe...

Option Explicit
Sub Timesheet_Format()
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
sht.select
'ACTIONS TO BE DONE
Next sht
End Sub

But for the most part, it's not necessary to select the worksheet to work with
it.

If you qualify your ranges (or whatever objects your actions work on), it may
work quicker.

Option Explicit
Sub Timesheet_Format()
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
sht.range("a1").clearcontents
'ACTIONS TO BE DONE
Next sht
End Sub



BSII wrote:

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...


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Selecting Next Worksheet

Your code is basically correct, but I suspect your understanding of it is
not. In a For Each loop, each worksheet is NOT activated in the loop. The
Active Sheet remains what it was before the code executes unless you
specifically make it the active sheet. For example, suppose Sheet1 is the
active sheet.

Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
Range("A1").Value = 123
Next WS

will change A1 on the Sheet1, NOT on each sheet in the loop. Instead, you
should use your control variable to reference the sheet in the loop. E.g.,

Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
WS.Range("A1").Value = 123
Next WS


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"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...



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Selecting Next Worksheet

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...



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Selecting Next Worksheet

Without seeing your "actions to be done code" hard to tell.

This code does the job as advertised.

Sub shtsprotect()
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
sht.Protect Password:="justme"
Next sht
End Sub


Gord Dibben MS Excel MVP


On Sat, 20 Jan 2007 17:56:02 -0800, BSII wrote:

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...




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Selecting Next Worksheet

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...




  #7   Report Post  
Posted to microsoft.public.excel.programming
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...






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Selecting Next Worksheet

Just a note...

You may want to make it so the module and the subroutine have different names.

PERSONAL.XLS!Mod_TimeSub2.TimeSub2

I've seen bad things happen when both the module and the procedure have the same
name.

BSII wrote:

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...





--

Dave Peterson
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
Selecting a row in a worksheet Rochelle in Melbourne Excel Worksheet Functions 1 April 9th 09 11:34 AM
Sorting a range on another worksheet without selecting the worksheet [email protected] Excel Programming 1 December 21st 06 11:06 PM
Help! Selecting a Worksheet with VBA [email protected] Excel Programming 7 December 18th 06 08:52 PM
selecting cell range in other worksheet without switching to worksheet suzetter[_4_] Excel Programming 4 June 22nd 05 08:55 PM
Selecting Last Worksheet Alex Excel Programming 7 September 26th 04 09:35 PM


All times are GMT +1. The time now is 04:29 PM.

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

About Us

"It's about Microsoft Excel"