+ Reply to Thread
Results 1 to 10 of 10

Deleting empty lines using a macro

  1. #1
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Deleting empty lines using a macro

    Hello,

    I have another report which needs to be sorted using a macro. However, before I can sort the data, I need to delete rows at the bottom of each report that have "0" values in column A and B. Again, each report I run is of different length so I need to write a code that will take that into consideration. Here is an example of what I need to do:

    Sub second_step()
    '
    ' second_step Macro
    '

    '
    Range("C1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Rows("7876:7876").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    Range("A1").Select
    End Sub

    Now, the thing is, not every report will start having "0" values in row 7876. How do I write a code to reflect this, please?

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Deleting empty lines using a macro

    Do you want the code to treat a blank cell as a zero or as a blank?

    This will not clear the row if either column A or B is blank:
    If Range("A" & i).Formula = 0 And Range("B" & i).Formula = 0 Then
    This will clear the row if either is column A or B blank:
    If Range("A" & i).Value = 0 And Range("B" & i).Value = 0 Then


    The entire code:

    Please Login or Register  to view this content.

    Here is the file I created while creating this:
    ClearRowContents.xlsm
    Last edited by mikeTRON; 08-07-2014 at 11:17 AM.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Re: Deleting empty lines using a macro

    Hi,

    Many thanks for your reply but this didn't work ...

    I have some data in columns A-G. Columns A and B then go on to have some zeros in the cells at the bottom of each report. I need to get rid of those "0" from column A and B from the bottom of my report as I then need to sort it.

    I really don't understand why the command Ctrl+Shift+arrow down doesn't work???

    Thanks!

  4. #4
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Re: Deleting empty lines using a macro

    Would an IF THEN command work for this?

    Something like IF there is a "0" in column A and B, THEN delete the entire row...

    ???

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Deleting empty lines using a macro

    Does this help?
    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Deleting empty lines using a macro

    Try this:
    Please Login or Register  to view this content.
    Cheers,
    berlan
    Last edited by berlan; 08-08-2014 at 09:33 AM.

  7. #7
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Re: Deleting empty lines using a macro

    Quote Originally Posted by berlan View Post
    Try this:
    Please Login or Register  to view this content.
    Cheers,
    berlan
    Hi,

    Thank you. It worked only on column A. When I tried running the same code on column B it didn't work....

  8. #8
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Deleting empty lines using a macro

    Post a sample data set with a proposed solution so we can SEE exactly what you are trying to do, because there is a disconnect between what you are saying and what you actually need, apparently.

  9. #9
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Deleting empty lines using a macro

    Quote Originally Posted by Dorocica View Post
    Would an IF THEN command work for this?

    Something like IF there is a "0" in column A and B, THEN delete the entire row...

    ???

    This is what I had in the code listed above, but I think you were unclear on your expectations. In the sample code you had CLEAR the row, not delete.
    Again, it helps a ton to have sample data and sample proposed solution so we can see exactly what you need.

  10. #10
    Registered User
    Join Date
    06-11-2014
    Posts
    12

    Re: Deleting empty lines using a macro

    Hello,

    And sorry. I suppose my lack of knowledge about excel/macros is quite transparent.

    I have attached a file that basically shows what I am trying to do. I have data in columns A to G and then "0" going quite a way down in column A and B.

    I would like to have a macro that gets rid of all those cells with "0" value.

    Thank you!
    Attached Files Attached Files

+ 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. Code/Macro for deleting empty columns and rows.
    By gautamacharya in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-09-2014, 01:49 PM
  2. Macro for adding/deleting new lines
    By lukaszK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2013, 05:46 AM
  3. [SOLVED] Deleting empty rows in excel using macro
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-26-2013, 02:40 AM
  4. How to insert empty lines and code using a Macro
    By Harmalarm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2008, 09:37 AM

Tags for this Thread

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