PC Review


Reply
Thread Tools Rate Thread

Code to delete rows where cell value is zero

 
 
=?Utf-8?B?QW5uIFNjaGFycGY=?=
Guest
Posts: n/a
 
      18th Oct 2006
I am working in an Excel sheet where data is dumped from another system.
Many of the rows are extraneous - if the cell in the Current Hours column
(AB) contains a zero value, I'd like my macro to delete the row. Someone
here at my office knows more VBA than I do (which is very little) and we came
up with the following. It is NOT working correctly.

The current sheet of test data contains 575 rows. Of these, 24 have values
greater than zero in column AB. 551 rows contain a zero in AB. When I run
the macro, it does not seem to actually be using the values in AB to
determine what to delete. Depending on which cell is active when I start,
sometimes there are values > 0 in AB, other times they are all = 0. I end up
with a different number of rows depending on where I start.

Here is the code of the macro. If anyone can tell me how to make the macro
look at the contents of the cells in column AB and delete when they contain
0, I would really appreciate it.

Thanks very much for your help.

Sub DelBlankCurHrsRows()
'
' DelBlankCurHrsRows Macro
' Macro recorded 10/18/2006 by Ann Scharpf
'
' Keyboard Shortcut: Ctrl+Shift+E
'
For Each c In Worksheets("Paste all employees' data
here").Range("AB2:AB41000")
If c.Value = "0" Then
Selection.EntireRow.Delete
End If
Next
End Sub




--
Ann Scharpf
 
Reply With Quote
 
 
 
 
Otto Moehrbach
Guest
Posts: n/a
 
      18th Oct 2006
Ann
When you are looping through a range and deleting rows, you want to be
sure you loop from the bottom up rather than from the top down. This macro
will do what you want, but your "blank" cells in Column AB must be actually
blank. I know you say Huh? at this point but when data is brought into
Excel from an external source, problems may come with the data. For
instance, a "blank" cell may have a space in it. That cell is not blank.
It is an occupied cell. I included code in this macro to Trim (remove all
extraneous spaces) all the cells in Column AB. Come back if this macro does
not delete ALL the "blank" cells. HTH Otto
Sub RemoveBlank()
Dim RngColAB As Range
Dim c As Long
Application.ScreenUpdating = False
Set RngColAB = Range("AB2", Range("AB" & Rows.Count).End(xlUp))
For c = RngColAB.Count To 1 Step -1
RngColAB(c).Value = Application.Trim(RngColAB(c))
If IsEmpty(RngColAB(c).Value) Then _
RngColAB(c).EntireRow.Delete
Next c
Application.ScreenUpdating = True
End Sub
"Ann Scharpf" <(E-Mail Removed)> wrote in message
news:2C84CE8B-9688-4235-9F7A-(E-Mail Removed)...
>I am working in an Excel sheet where data is dumped from another system.
> Many of the rows are extraneous - if the cell in the Current Hours column
> (AB) contains a zero value, I'd like my macro to delete the row. Someone
> here at my office knows more VBA than I do (which is very little) and we
> came
> up with the following. It is NOT working correctly.
>
> The current sheet of test data contains 575 rows. Of these, 24 have
> values
> greater than zero in column AB. 551 rows contain a zero in AB. When I
> run
> the macro, it does not seem to actually be using the values in AB to
> determine what to delete. Depending on which cell is active when I start,
> sometimes there are values > 0 in AB, other times they are all = 0. I end
> up
> with a different number of rows depending on where I start.
>
> Here is the code of the macro. If anyone can tell me how to make the
> macro
> look at the contents of the cells in column AB and delete when they
> contain
> 0, I would really appreciate it.
>
> Thanks very much for your help.
>
> Sub DelBlankCurHrsRows()
> '
> ' DelBlankCurHrsRows Macro
> ' Macro recorded 10/18/2006 by Ann Scharpf
> '
> ' Keyboard Shortcut: Ctrl+Shift+E
> '
> For Each c In Worksheets("Paste all employees' data
> here").Range("AB2:AB41000")
> If c.Value = "0" Then
> Selection.EntireRow.Delete
> End If
> Next
> End Sub
>
>
>
>
> --
> Ann Scharpf



 
Reply With Quote
 
=?Utf-8?B?QW5uIFNjaGFycGY=?=
Guest
Posts: n/a
 
      18th Oct 2006
Hi, Otto:

Thanks for taking the time to try to help me.

I always end up causing confusion when I try to "alter the facts" for
brevity. Actually, this data is coming from other Excel sheets. We have 100
employees who fill out bi-weekly status reports. I have written macros to
copy the region where an employee MIGHT have entered data (never know how
many rows the employee will use week to week) and dump the data (Paste as
values) into an aggregate sheet. Further macros are eliminating columns that
are no longer needed and I would like to delete these rows that calculated
zero totals for the cumulative hours. The rows that I want to delete really
DO contain a zero in the Cumulative Hours cell.

I did add your code into my macros. When I run RemoveBlank, it doesn't seem
to do anything at all. Was I supposed to add something before or after this
code?

Thanks again.

Ann
--
Ann Scharpf


"Otto Moehrbach" wrote:

> Ann
> When you are looping through a range and deleting rows, you want to be
> sure you loop from the bottom up rather than from the top down. This macro
> will do what you want, but your "blank" cells in Column AB must be actually
> blank. I know you say Huh? at this point but when data is brought into
> Excel from an external source, problems may come with the data. For
> instance, a "blank" cell may have a space in it. That cell is not blank.
> It is an occupied cell. I included code in this macro to Trim (remove all
> extraneous spaces) all the cells in Column AB. Come back if this macro does
> not delete ALL the "blank" cells. HTH Otto
> Sub RemoveBlank()
> Dim RngColAB As Range
> Dim c As Long
> Application.ScreenUpdating = False
> Set RngColAB = Range("AB2", Range("AB" & Rows.Count).End(xlUp))
> For c = RngColAB.Count To 1 Step -1
> RngColAB(c).Value = Application.Trim(RngColAB(c))
> If IsEmpty(RngColAB(c).Value) Then _
> RngColAB(c).EntireRow.Delete
> Next c
> Application.ScreenUpdating = True
> End Sub
> "Ann Scharpf" <(E-Mail Removed)> wrote in message
> news:2C84CE8B-9688-4235-9F7A-(E-Mail Removed)...
> >I am working in an Excel sheet where data is dumped from another system.
> > Many of the rows are extraneous - if the cell in the Current Hours column
> > (AB) contains a zero value, I'd like my macro to delete the row. Someone
> > here at my office knows more VBA than I do (which is very little) and we
> > came
> > up with the following. It is NOT working correctly.
> >
> > The current sheet of test data contains 575 rows. Of these, 24 have
> > values
> > greater than zero in column AB. 551 rows contain a zero in AB. When I
> > run
> > the macro, it does not seem to actually be using the values in AB to
> > determine what to delete. Depending on which cell is active when I start,
> > sometimes there are values > 0 in AB, other times they are all = 0. I end
> > up
> > with a different number of rows depending on where I start.
> >
> > Here is the code of the macro. If anyone can tell me how to make the
> > macro
> > look at the contents of the cells in column AB and delete when they
> > contain
> > 0, I would really appreciate it.
> >
> > Thanks very much for your help.
> >
> > Sub DelBlankCurHrsRows()
> > '
> > ' DelBlankCurHrsRows Macro
> > ' Macro recorded 10/18/2006 by Ann Scharpf
> > '
> > ' Keyboard Shortcut: Ctrl+Shift+E
> > '
> > For Each c In Worksheets("Paste all employees' data
> > here").Range("AB2:AB41000")
> > If c.Value = "0" Then
> > Selection.EntireRow.Delete
> > End If
> > Next
> > End Sub
> >
> >
> >
> >
> > --
> > Ann Scharpf

>
>
>

 
Reply With Quote
 
=?Utf-8?B?QW5uIFNjaGFycGY=?=
Guest
Posts: n/a
 
      18th Oct 2006
I reread your message and changed the code as follows:

If IsEmpty(RngColAB(c).Value) Then _
changed to
If RngColAB(c).Value = 0 Then _

This made the macro work. It deletes all but the 24 rows that have a value
> 0. Thanks very much for your help. Now I will READ the code and try to

understand it!

--
Ann Scharpf


"Otto Moehrbach" wrote:

> Ann
> When you are looping through a range and deleting rows, you want to be
> sure you loop from the bottom up rather than from the top down. This macro
> will do what you want, but your "blank" cells in Column AB must be actually
> blank. I know you say Huh? at this point but when data is brought into
> Excel from an external source, problems may come with the data. For
> instance, a "blank" cell may have a space in it. That cell is not blank.
> It is an occupied cell. I included code in this macro to Trim (remove all
> extraneous spaces) all the cells in Column AB. Come back if this macro does
> not delete ALL the "blank" cells. HTH Otto
> Sub RemoveBlank()
> Dim RngColAB As Range
> Dim c As Long
> Application.ScreenUpdating = False
> Set RngColAB = Range("AB2", Range("AB" & Rows.Count).End(xlUp))
> For c = RngColAB.Count To 1 Step -1
> RngColAB(c).Value = Application.Trim(RngColAB(c))
> If IsEmpty(RngColAB(c).Value) Then _
> RngColAB(c).EntireRow.Delete
> Next c
> Application.ScreenUpdating = True
> End Sub
> "Ann Scharpf" <(E-Mail Removed)> wrote in message
> news:2C84CE8B-9688-4235-9F7A-(E-Mail Removed)...
> >I am working in an Excel sheet where data is dumped from another system.
> > Many of the rows are extraneous - if the cell in the Current Hours column
> > (AB) contains a zero value, I'd like my macro to delete the row. Someone
> > here at my office knows more VBA than I do (which is very little) and we
> > came
> > up with the following. It is NOT working correctly.
> >
> > The current sheet of test data contains 575 rows. Of these, 24 have
> > values
> > greater than zero in column AB. 551 rows contain a zero in AB. When I
> > run
> > the macro, it does not seem to actually be using the values in AB to
> > determine what to delete. Depending on which cell is active when I start,
> > sometimes there are values > 0 in AB, other times they are all = 0. I end
> > up
> > with a different number of rows depending on where I start.
> >
> > Here is the code of the macro. If anyone can tell me how to make the
> > macro
> > look at the contents of the cells in column AB and delete when they
> > contain
> > 0, I would really appreciate it.
> >
> > Thanks very much for your help.
> >
> > Sub DelBlankCurHrsRows()
> > '
> > ' DelBlankCurHrsRows Macro
> > ' Macro recorded 10/18/2006 by Ann Scharpf
> > '
> > ' Keyboard Shortcut: Ctrl+Shift+E
> > '
> > For Each c In Worksheets("Paste all employees' data
> > here").Range("AB2:AB41000")
> > If c.Value = "0" Then
> > Selection.EntireRow.Delete
> > End If
> > Next
> > End Sub
> >
> >
> >
> >
> > --
> > Ann Scharpf

>
>
>

 
Reply With Quote
 
Cheekyaardvark
Guest
Posts: n/a
 
      7th Dec 2006
Otto...
Not sure if I posted this correctly last time.. but I was wanting to
take the macro here one step further & have it so that if I highlight
any column in any spreadsheet that it would remove any rows with zero
value in that column... I had macros before that simply utilised
autofilter but with Excel 2003 when using large files excel just seems
to hang

Can you give me some guidance..
Much Appreciated

Robin


Otto Moehrbach wrote:

> Ann
> When you are looping through a range and deleting rows, you want to be
> sure you loop from the bottom up rather than from the top down. This macro
> will do what you want, but your "blank" cells in Column AB must be actually
> blank. I know you say Huh? at this point but when data is brought into
> Excel from an external source, problems may come with the data. For
> instance, a "blank" cell may have a space in it. That cell is not blank.
> It is an occupied cell. I included code in this macro to Trim (remove all
> extraneous spaces) all the cells in Column AB. Come back if this macro does
> not delete ALL the "blank" cells. HTH Otto
> Sub RemoveBlank()
> Dim RngColAB As Range
> Dim c As Long
> Application.ScreenUpdating = False
> Set RngColAB = Range("AB2", Range("AB" & Rows.Count).End(xlUp))
> For c = RngColAB.Count To 1 Step -1
> RngColAB(c).Value = Application.Trim(RngColAB(c))
> If IsEmpty(RngColAB(c).Value) Then _
> RngColAB(c).EntireRow.Delete
> Next c
> Application.ScreenUpdating = True
> End Sub
> "Ann Scharpf" <(E-Mail Removed)> wrote in message
> news:2C84CE8B-9688-4235-9F7A-(E-Mail Removed)...
> >I am working in an Excel sheet where data is dumped from another system.
> > Many of the rows are extraneous - if the cell in the Current Hours column
> > (AB) contains a zero value, I'd like my macro to delete the row. Someone
> > here at my office knows more VBA than I do (which is very little) and we
> > came
> > up with the following. It is NOT working correctly.
> >
> > The current sheet of test data contains 575 rows. Of these, 24 have
> > values
> > greater than zero in column AB. 551 rows contain a zero in AB. When I
> > run
> > the macro, it does not seem to actually be using the values in AB to
> > determine what to delete. Depending on which cell is active when I start,
> > sometimes there are values > 0 in AB, other times they are all = 0. I end
> > up
> > with a different number of rows depending on where I start.
> >
> > Here is the code of the macro. If anyone can tell me how to make the
> > macro
> > look at the contents of the cells in column AB and delete when they
> > contain
> > 0, I would really appreciate it.
> >
> > Thanks very much for your help.
> >
> > Sub DelBlankCurHrsRows()
> > '
> > ' DelBlankCurHrsRows Macro
> > ' Macro recorded 10/18/2006 by Ann Scharpf
> > '
> > ' Keyboard Shortcut: Ctrl+Shift+E
> > '
> > For Each c In Worksheets("Paste all employees' data
> > here").Range("AB2:AB41000")
> > If c.Value = "0" Then
> > Selection.EntireRow.Delete
> > End If
> > Next
> > End Sub
> >
> >
> >
> >
> > --
> > Ann Scharpf


 
Reply With Quote
 
Otto Moehrbach
Guest
Posts: n/a
 
      7th Dec 2006
Robin
By "remove" do you mean hide or delete the row? If a row is deleted
it's gone forever once the file is saved. If the row is hidden, it can be
simply un-hidden later and it's back. Start a new post or contact me
direct. My Outlook Express is listing your posts back with the thread in
October and I'm losing you. My email address is (E-Mail Removed).
Remove the "nop" from this address. HTH Otto
"Cheekyaardvark" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Otto...
> Not sure if I posted this correctly last time.. but I was wanting to
> take the macro here one step further & have it so that if I highlight
> any column in any spreadsheet that it would remove any rows with zero
> value in that column... I had macros before that simply utilised
> autofilter but with Excel 2003 when using large files excel just seems
> to hang
>
> Can you give me some guidance..
> Much Appreciated
>
> Robin
>
>
> Otto Moehrbach wrote:
>
>> Ann
>> When you are looping through a range and deleting rows, you want to
>> be
>> sure you loop from the bottom up rather than from the top down. This
>> macro
>> will do what you want, but your "blank" cells in Column AB must be
>> actually
>> blank. I know you say Huh? at this point but when data is brought into
>> Excel from an external source, problems may come with the data. For
>> instance, a "blank" cell may have a space in it. That cell is not blank.
>> It is an occupied cell. I included code in this macro to Trim (remove
>> all
>> extraneous spaces) all the cells in Column AB. Come back if this macro
>> does
>> not delete ALL the "blank" cells. HTH Otto
>> Sub RemoveBlank()
>> Dim RngColAB As Range
>> Dim c As Long
>> Application.ScreenUpdating = False
>> Set RngColAB = Range("AB2", Range("AB" & Rows.Count).End(xlUp))
>> For c = RngColAB.Count To 1 Step -1
>> RngColAB(c).Value = Application.Trim(RngColAB(c))
>> If IsEmpty(RngColAB(c).Value) Then _
>> RngColAB(c).EntireRow.Delete
>> Next c
>> Application.ScreenUpdating = True
>> End Sub
>> "Ann Scharpf" <(E-Mail Removed)> wrote in message
>> news:2C84CE8B-9688-4235-9F7A-(E-Mail Removed)...
>> >I am working in an Excel sheet where data is dumped from another system.
>> > Many of the rows are extraneous - if the cell in the Current Hours
>> > column
>> > (AB) contains a zero value, I'd like my macro to delete the row.
>> > Someone
>> > here at my office knows more VBA than I do (which is very little) and
>> > we
>> > came
>> > up with the following. It is NOT working correctly.
>> >
>> > The current sheet of test data contains 575 rows. Of these, 24 have
>> > values
>> > greater than zero in column AB. 551 rows contain a zero in AB. When I
>> > run
>> > the macro, it does not seem to actually be using the values in AB to
>> > determine what to delete. Depending on which cell is active when I
>> > start,
>> > sometimes there are values > 0 in AB, other times they are all = 0. I
>> > end
>> > up
>> > with a different number of rows depending on where I start.
>> >
>> > Here is the code of the macro. If anyone can tell me how to make the
>> > macro
>> > look at the contents of the cells in column AB and delete when they
>> > contain
>> > 0, I would really appreciate it.
>> >
>> > Thanks very much for your help.
>> >
>> > Sub DelBlankCurHrsRows()
>> > '
>> > ' DelBlankCurHrsRows Macro
>> > ' Macro recorded 10/18/2006 by Ann Scharpf
>> > '
>> > ' Keyboard Shortcut: Ctrl+Shift+E
>> > '
>> > For Each c In Worksheets("Paste all employees' data
>> > here").Range("AB2:AB41000")
>> > If c.Value = "0" Then
>> > Selection.EntireRow.Delete
>> > End If
>> > Next
>> > End Sub
>> >
>> >
>> >
>> >
>> > --
>> > Ann Scharpf

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Rows if any cell in Column H is blank but do not Delete Fir =?Utf-8?B?bWFuZmFyZWVk?= Microsoft Excel Programming 4 28th Sep 2007 05:20 PM
VBA code to delete rows =?Utf-8?B?U2VjcmV0IFNxdWlycmVs?= Microsoft Excel Misc 3 21st Jan 2007 03:01 PM
code to delete rows =?Utf-8?B?aWFuYWxleGg=?= Microsoft Excel Misc 5 5th May 2005 10:46 AM
Code to delete rows Ivor Williams Microsoft Access Forms 1 1st May 2005 06:31 PM
Code to delete rows not working CrankyLemming Microsoft Excel Misc 7 23rd Aug 2004 05:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 PM.