Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default For Each...won't loop through worksheets

What am I missing here? Below is some copy/pasted code in an attempt to have
a Sub run on each worksheet of a workbook. I have some more involved
reformatting routines that I'd like to run as well on these sheets, but I
figured I'd start here. I can't seem to get past a basic point.

I know there are more ways than one to skin this cat & I'm not in good form
w/o declarations - please indulge me...

When this code runs in a workbook with worksheets named:

Summary, Sun, Mon, Tue, Wed, Thu, Fri, Sat

....it only does anything if the active sheet is something other than
'Summary' and then it deletes 7 rows from the Active sheet. What am I
missing that's not allowing the first row of each sheet to be deleted (I
actually want to delete the first 11).

Sub DeleteOldHeader()

Dim sht As Worksheet

Dim target As Worksheet

Set target = ActiveWorkbook.Worksheets("Summary")

For Each sht In Worksheets

If sht.Name < target.Name Then

Rows(1).Delete

End If

Next sht

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default For Each...won't loop through worksheets

try replacing this line

For Each sht In Worksheets

with this

For Each sht In Activeworkbook.Worksheets

HTH


Bill G wrote:
What am I missing here? Below is some copy/pasted code in an attempt to have
a Sub run on each worksheet of a workbook. I have some more involved
reformatting routines that I'd like to run as well on these sheets, but I
figured I'd start here. I can't seem to get past a basic point.

I know there are more ways than one to skin this cat & I'm not in good form
w/o declarations - please indulge me...

When this code runs in a workbook with worksheets named:

Summary, Sun, Mon, Tue, Wed, Thu, Fri, Sat

...it only does anything if the active sheet is something other than
'Summary' and then it deletes 7 rows from the Active sheet. What am I
missing that's not allowing the first row of each sheet to be deleted (I
actually want to delete the first 11).

Sub DeleteOldHeader()

Dim sht As Worksheet

Dim target As Worksheet

Set target = ActiveWorkbook.Worksheets("Summary")

For Each sht In Worksheets

If sht.Name < target.Name Then

Rows(1).Delete

End If

Next sht

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default For Each...won't loop through worksheets

The issue that you are running into is that if you are not working with the
active sheet then you need to be specific as to which sheet you want to
modify... Where you try to delete the row you do not specifiy which sheet so
the action is performed on the active sheet. Give this a try...

Sub DeleteOldHeader()
Dim sht As Worksheet
Dim target As Worksheet
Set target = ActiveWorkbook.Worksheets("Summary")
For Each sht In Worksheets
If sht.Name < target.Name Then
sht.Rows(1).Delete
End If
Next sht
End Sub


--
HTH...

Jim Thomlinson


"Bill G" wrote:

What am I missing here? Below is some copy/pasted code in an attempt to have
a Sub run on each worksheet of a workbook. I have some more involved
reformatting routines that I'd like to run as well on these sheets, but I
figured I'd start here. I can't seem to get past a basic point.

I know there are more ways than one to skin this cat & I'm not in good form
w/o declarations - please indulge me...

When this code runs in a workbook with worksheets named:

Summary, Sun, Mon, Tue, Wed, Thu, Fri, Sat

....it only does anything if the active sheet is something other than
'Summary' and then it deletes 7 rows from the Active sheet. What am I
missing that's not allowing the first row of each sheet to be deleted (I
actually want to delete the first 11).

Sub DeleteOldHeader()

Dim sht As Worksheet

Dim target As Worksheet

Set target = ActiveWorkbook.Worksheets("Summary")

For Each sht In Worksheets

If sht.Name < target.Name Then

Rows(1).Delete

End If

Next sht

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default For Each...won't loop through worksheets

Thanks so much - now I'll see if the rest of my knows-enough-to-be-dangerous
stuff works.

This group is very much appreciated.


The issue that you are running into is that if you are not working with
the
active sheet then you need to be specific as to which sheet you want to
modify... Where you try to delete the row you do not specifiy which sheet
so
the action is performed on the active sheet. Give this a try...

Sub DeleteOldHeader()
Dim sht As Worksheet
Dim target As Worksheet
Set target = ActiveWorkbook.Worksheets("Summary")
For Each sht In Worksheets
If sht.Name < target.Name Then
sht.Rows(1).Delete
End If
Next sht
End Sub


--
HTH...

Jim Thomlinson


"Bill G" wrote:

What am I missing here? Below is some copy/pasted code in an attempt to
have
a Sub run on each worksheet of a workbook. I have some more involved
reformatting routines that I'd like to run as well on these sheets, but I
figured I'd start here. I can't seem to get past a basic point.

I know there are more ways than one to skin this cat & I'm not in good
form
w/o declarations - please indulge me...

When this code runs in a workbook with worksheets named:

Summary, Sun, Mon, Tue, Wed, Thu, Fri, Sat

....it only does anything if the active sheet is something other than
'Summary' and then it deletes 7 rows from the Active sheet. What am I
missing that's not allowing the first row of each sheet to be deleted (I
actually want to delete the first 11).

Sub DeleteOldHeader()

Dim sht As Worksheet

Dim target As Worksheet

Set target = ActiveWorkbook.Worksheets("Summary")

For Each sht In Worksheets

If sht.Name < target.Name Then

Rows(1).Delete

End If

Next sht

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 worksheets merry_fay Excel Discussion (Misc queries) 2 January 7th 09 01:54 PM
Naming worksheets in loop Jenni_Sweden Excel Programming 3 June 15th 06 12:59 PM
Loop thru some worksheets Mike Fogleman Excel Programming 2 August 21st 05 12:22 AM
Loop Through worksheets Stephen[_9_] Excel Programming 5 April 20th 04 01:46 PM
Loop worksheets Steve[_35_] Excel Programming 3 November 25th 03 04:46 AM


All times are GMT +1. The time now is 05:26 AM.

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"