Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default For each worksheet in workbook problem

I'm having trouble with a for each statement.
The code below is intended to clear all data (starting with rows with
numbers) on each worksheet except sheet1.
If I run the macro while on sheet1, it works like a charm.
If I run the macro while on any other sheet, it only clears the data from
that sheet.
I can't figure out why.

---------------------------
Sub ClearAll()
Dim w As Worksheet
Dim wb As Workbook
Dim T As Long
Dim FirstRow As Long
Dim LastRow As Long

Set wb = ActiveWorkbook
Application.ScreenUpdating = False

For Each w In wb.Worksheets
If Not w.Name = Sheet1.Name Then
FirstRow = 0
LastRow = Cells(w.Rows.Count, "A").End(xlUp).Row
With w
T = 1
Do Until FirstRow < 0
If IsNumeric(w.Cells(T, "A")) Then
If w.Cells(T, "A").Value 0 Then
FirstRow = T
ElseIf T = LastRow Then FirstRow = LastRow
ElseIf T 50 Then FirstRow = 1
End If
Else: FirstRow = 0
End If
T = T + 1
Loop
w.Range("A" & FirstRow & ":A" & LastRow).EntireRow.Delete
End With
End If
Next

Application.ScreenUpdating = True
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default For each worksheet in workbook problem

LastRow = w.Cells(w.Rows.Count, "A").End(xlUp).Row

"elf27" wrote in message
...
I'm having trouble with a for each statement.
The code below is intended to clear all data (starting with rows with
numbers) on each worksheet except sheet1.
If I run the macro while on sheet1, it works like a charm.
If I run the macro while on any other sheet, it only clears the data from
that sheet.
I can't figure out why.

---------------------------
Sub ClearAll()
Dim w As Worksheet
Dim wb As Workbook
Dim T As Long
Dim FirstRow As Long
Dim LastRow As Long

Set wb = ActiveWorkbook
Application.ScreenUpdating = False

For Each w In wb.Worksheets
If Not w.Name = Sheet1.Name Then
FirstRow = 0
LastRow = Cells(w.Rows.Count, "A").End(xlUp).Row
With w
T = 1
Do Until FirstRow < 0
If IsNumeric(w.Cells(T, "A")) Then
If w.Cells(T, "A").Value 0 Then
FirstRow = T
ElseIf T = LastRow Then FirstRow = LastRow
ElseIf T 50 Then FirstRow = 1
End If
Else: FirstRow = 0
End If
T = T + 1
Loop
w.Range("A" & FirstRow & ":A" & LastRow).EntireRow.Delete
End With
End If
Next

Application.ScreenUpdating = True
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default For each worksheet in workbook problem

Brilliant, Charles.
Thanks!
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
Problem w/code to copy worksheet to new workbook ploddinggaltn Excel Programming 9 October 29th 06 10:00 PM
problem with formatting text in every worksheet same workbook Moon Excel Programming 2 November 2nd 05 03:30 PM
Copy sheet from one workbook to another workbook problem Ron de Bruin Excel Programming 3 August 5th 04 07:19 PM
Workbook crashes after renaming a worksheet - References problem? BenD[_2_] Excel Programming 0 April 21st 04 01:22 AM


All times are GMT +1. The time now is 08:26 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"