Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Help revising slow code

I am using the following code to remove all the lines in a worksheet that
have a zero value in Column H. Because there are thousands of rows in the
worksheet, this loop takes forever! Does anyone have any suggestions for
doing this more quickly? Thanks!

Sub Delete_Row_w0()
Dim Firstrow As Long
Dim LastRow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With Sheets(1)
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = LastRow To Firstrow Step -1

With .Cells(Lrow, "H")

If Not IsError(.Value) Then

If .Value = 0 Then .EntireRow.Delete

End If

End With

Next Lrow

End With

ActiveWindow.View = ViewMode
With Application
.Calculation = CalcMode
End With

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Help revising slow code

The following suggested macros assume Column H contains constant values and
not formulas that display values. With that said, IF the cells in Column H
will NEVER be blank when there is data in other cells on that row, then you
can use this macro...

Sub RemoveZeroesFromColumnHAsLongAsThereAreNoBlanksInC olumnH()
With Columns("H")
.Replace "0", "", xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

If, however, there could be blanks in Column H on rows that have data
elsewhere, then try this macro instead...

Sub RemoveZeroesFromColumnH()
Dim LastRow As Long
LastRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
With Range("H1:H" & LastRow)
.Replace "", Chr(255), xlWhole
.Replace "0", "", xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
.Replace Chr(255), "", xlWhole
End With
End Sub

As with all macros you are trying out for the first time, it is advisable to
do your test on a copy of your worksheet and not on the actual worksheet
containing your data... changes made by a macro cannot be undone.

--
Rick (MVP - Excel)



"Dani" wrote in message
...
I am using the following code to remove all the lines in a worksheet that
have a zero value in Column H. Because there are thousands of rows in the
worksheet, this loop takes forever! Does anyone have any suggestions for
doing this more quickly? Thanks!

Sub Delete_Row_w0()
Dim Firstrow As Long
Dim LastRow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With Sheets(1)
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = LastRow To Firstrow Step -1

With .Cells(Lrow, "H")

If Not IsError(.Value) Then

If .Value = 0 Then .EntireRow.Delete

End If

End With

Next Lrow

End With

ActiveWindow.View = ViewMode
With Application
.Calculation = CalcMode
End With

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Help revising slow code

One thing I forgot... we need to protect the code against there being no
zeroes in Column H. Put the following statement in as the FIRST line of code
in whichever macro of mine that you use...

On Error Resume Next

--
Rick (MVP - Excel)



"Rick Rothstein" wrote in message
...
The following suggested macros assume Column H contains constant values
and not formulas that display values. With that said, IF the cells in
Column H will NEVER be blank when there is data in other cells on that
row, then you can use this macro...

Sub RemoveZeroesFromColumnHAsLongAsThereAreNoBlanksInC olumnH()
With Columns("H")
.Replace "0", "", xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

If, however, there could be blanks in Column H on rows that have data
elsewhere, then try this macro instead...

Sub RemoveZeroesFromColumnH()
Dim LastRow As Long
LastRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
With Range("H1:H" & LastRow)
.Replace "", Chr(255), xlWhole
.Replace "0", "", xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
.Replace Chr(255), "", xlWhole
End With
End Sub

As with all macros you are trying out for the first time, it is advisable
to do your test on a copy of your worksheet and not on the actual
worksheet containing your data... changes made by a macro cannot be
undone.

--
Rick (MVP - Excel)



"Dani" wrote in message
...
I am using the following code to remove all the lines in a worksheet that
have a zero value in Column H. Because there are thousands of rows in the
worksheet, this loop takes forever! Does anyone have any suggestions for
doing this more quickly? Thanks!

Sub Delete_Row_w0()
Dim Firstrow As Long
Dim LastRow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With Sheets(1)
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = LastRow To Firstrow Step -1

With .Cells(Lrow, "H")

If Not IsError(.Value) Then

If .Value = 0 Then .EntireRow.Delete

End If

End With

Next Lrow

End With

ActiveWindow.View = ViewMode
With Application
.Calculation = CalcMode
End With

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
Fast code in 2003 = agonizingly slow code in 2007 XP Excel Programming 25 October 21st 08 01:01 PM
excel 2003 - revising a formula to make it more accurate Doug Excel Worksheet Functions 3 June 30th 08 12:54 AM
Slow code when used as VBA code instead of macro (copying visible columns) [email protected] Excel Programming 3 April 2nd 07 05:26 PM
need help revising day[_2_] Excel Programming 4 March 1st 06 02:25 PM
Need help revising print range selection Joel Mills Excel Programming 4 October 29th 04 09:53 PM


All times are GMT +1. The time now is 09:31 PM.

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"