+ Reply to Thread
Results 1 to 35 of 35

Macro to remove rows with zero values in a column

  1. #1
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Macro to remove rows with zero values in a column

    Hi All,

    Looking for some quick running code to remove rows containing a zero value in column Z.

    Row 1 contains column headings and Row 2 downwards contains the data.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro to remove rows with zero values in a column

    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  4. #4
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    wow that was fast. I get a debug error Runtime error 1004 no cells found on this bit of code

    Please Login or Register  to view this content.
    Not sure if it makes a difference but column Z is a formula

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    The code does not find empty cells. You can trap the error, but the code might not delete the rows.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    Quote Originally Posted by OllyXLS View Post
    Please Login or Register  to view this content.
    Thanks for this. Looks like it is working but had to stop it as it was taking a long time to run.

    I've turned screen updating and auto calculate off but seems to be running incredibly slowly. Any tips to speed it up? Applying it to about 5000 rows of data

  8. #8
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    Quote Originally Posted by AB33 View Post
    The code does not find empty cells. You can trap the error, but the code might not delete the rows.

    Please Login or Register  to view this content.
    it runs through now but doesn't delete any rows

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    Yes, because you have got empty string, but not empty cells. Did you try the filter?

    Last try
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    You could try this, might be a bit quicker:
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    AB33 both sets of code run through to the end but don't do anything?

    Yudlugar seems to be a bit quicker and works 100% but stills takes > 5 minutes. Wondering how i can speed this up somehow.
    Last edited by Gti182; 11-12-2013 at 06:50 AM.

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    How long does your sheet take to recalculate? I tested it on 7000+ rows and it was a few seconds.

    what value do you get for ActiveSheet.UsedRange.Rows.count?

  13. #13
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    5890 rows at the moment, takes +- 2 seconds to fully recalculate the workbook

  14. #14
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    How long does it take just for this:
    Please Login or Register  to view this content.
    ?

  15. #15
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    4-5 seconds

  16. #16
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    So it must be this line:
    Please Login or Register  to view this content.
    that is taking time.

    I don't see what would be quicker than selecting all the rows and deleting them in one go, apart from turning off calculation as you mention I don't think you can speed it up much more. What is the code you are using to turn the calculation off?

  17. #17
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    I just tried filtering and selecting zero value rows only then manually deleting with manual calculation on and that takes +- 3 min.

    I added this extra to your code.

    Please Login or Register  to view this content.

  18. #18
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    Just thinking out loud but is this any quicker:
    Please Login or Register  to view this content.

  19. #19
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    Try,

    Please Login or Register  to view this content.
    Last edited by AB33; 11-12-2013 at 03:03 PM.

  20. #20
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    thanks for all the help peeps! AB33 that code works well

  21. #21
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    AB33's code #19 looks identical to the code in post #10 to me. Are you ssaying the code in #19 is faster than that in #10?

    If so, anyone got any pointers as to why?

    Did you try the code in post #18 to see if it ran quicker?

  22. #22
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    code in #19 isn't much faster if at all.

    code #18 created another tab with unformatted data but didn't remove any rows from original data tab

  23. #23
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    Code#18 wasn't set up to solve the problem. I was wondering if it was faster to run. If so, it might be quicker to copy the rows you want into a new sheet, delete the original and rename the new sheet. Seeing as it was deleting the rows that seemed to be the problem.

  24. #24
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    Quote Originally Posted by yudlugar View Post
    Code#18 wasn't set up to solve the problem. I was wondering if it was faster to run. If so, it might be quicker to copy the rows you want into a new sheet, delete the original and rename the new sheet. Seeing as it was deleting the rows that seemed to be the problem.
    ah i understand now. Yes think it runs a bit faster but original data sheet has some formulas and formatting i'd like to keep so that way wouldn't really be worth it. I guess i'll just have to live with the running time, probably not the end of the world

  25. #25
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    Ah ok, the code I gave you will have copied the formulas but not formats you could try:
    Please Login or Register  to view this content.
    But that might take longer. How consistent are the formats? What do they look like, it shouldn't be too difficult to reapply them...

  26. #26
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    hmm formatting is a mixed bag of dates, values, text, etc. not too hard to replicate.

    The quickest way to do it manually is sorting the value column by smallest to largest or largest to smallest and then manually deleting all the zero value rows. Might be tricky to automate this to find the range of all the zero value rows though

  27. #27
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to remove rows with zero values in a column

    Is it consistent in each column?

    Any chance you can upload a workbook? Just a few rows to demonstrate what the formatting looks like, the data itself can be replaced with non-sensitive info if neccessary.

    What you mention as the quickest way manually is what the code in #10 and #19 does. I think it might be quicker this way though.

  28. #28
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    test file attached with formatting

    Test.xlsx

    please note there are about 30 other tabs which feed off this single tab which have been removed

  29. #29
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    Try. This is faster
    Attached Files Attached Files

  30. #30
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    takes pretty much the same time as the others.

    I think the thing that's causing the processing time to be so long is the actual deleting of the filtered zero rows. If they were sorted rather than filtered and then zero rows deleted it would be immediate
    Last edited by Gti182; 11-13-2013 at 09:41 AM.

  31. #31
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    double post oops

  32. #32
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    It is inconceivable a loop and filter could take the same amount of time. Yes, sorting can make a difference, but without it ,filter is still much faster than any loop.

    I think the issue may be with the type of data you have rather codes. Are these data imported from a site, or Accounting applications, such as SAP? If so, then you need to clean these data first.

  33. #33
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    the data is an export from oracle into an excel file. I then cut and paste this data to a model and copy format/formula's down from rows above.

  34. #34
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to remove rows with zero values in a column

    Ohm! I knew that.

    Data imported from Oracle have lots of unseen characters. Yes, copying and pasting VALUES ONLY will get rid most of these dirty data, but not all. What you need is to trim and clean data as well. You should not also put the imported data with the clean data. Clean the data first, then delete that sheet and save the new one on its own. I have almost lost my laptop on 3 occasions because of this issue.
    I have amended the code with sort code.
    Attached Files Attached Files

  35. #35
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    457

    Re: Macro to remove rows with zero values in a column

    BOOM! AB33 took +- 1 second to run and works perfectly thanks for all the help really appreciate it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to find duplicate values in a column and add the corresponding rows
    By hoss88 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-25-2013, 05:40 AM
  2. Remove rows with duplicate values in one column based on value of another column
    By jolleyje in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-15-2013, 06:20 AM
  3. excel macro to remove specific columns and rows + remove duplicate
    By garrywelson in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-17-2013, 12:03 PM
  4. Macro to remove rows of data when cells in a column contain certain text
    By RF8899 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2012, 07:35 PM
  5. Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range
    By ExcelQuestFL in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-19-2010, 09:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1