Forum Discussion

Philip Jen's avatar
Philip Jen
Copper Contributor
Sep 15, 2017

How do I delete unused rows in a macro?

I have a sheet into which I record new customer enquiries in consecutive rows.

 

Formula in the same row in subsequent columns pick out keywords and construct an appropriate reply from concatenated strings. Because the number of enquiries can vary from a ZERO to 100 on a daily basis, the formula is repeated in 100 rows.

 

When all the  enquiries have been entered, I run a macro, part of which COPIES the VALUES in the 100 consecutive rows and PASTES the output into a master sheet.

 

The problem is this method produces swaves of blank lines in the master sheet. Has anyone any idea how to construct the macro to eliminate the unused lines?

 

Rows("4:100").Select

Application.CutCopyMode = False

Selection.Copy

Sheets("Customers").Select

Rows("4:4").Select

Selection.Insert Shift:=xlDown

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=False

 

I've tried setting SkipBlanks:=  _True but one of the columns always returns the MATCH #N/A error, so the row is never blank.

=IF(MATCH(B4,Customers!B$1:B$292,0)>0,"**STOP**","")

 

 

 

 

 

4 Replies

  • The easiest way is to use a Filter on a column that has your delete criteria, then Go To (F5) > Special > Visible cells only > Delete rows > turn off the filter. All of which is easily recordable.

    You can post the resulting code back here, and someone can help you clean it up.

    I'd also set your data range up as a table. That way Excel won't care if you have 10 or 100 rows on any given day. A table will automatically extend itself (and formulas), as you enter new data.
    • Philip Jen's avatar
      Philip Jen
      Copper Contributor

      Smitty Smith wrote:
      The easiest way is to use a Filter on a column that has your delete criteria, then Go To (F5) > Special > Visible cells only > Delete rows > turn off the filter. All of which is easily recordable.

      ... thanks for this. Didn't think of filters so I'm working on it. After selecting "Visible cells only >" I'm not getting "Delete rows" but XThe selection already contains only visible cells"
      As I say, I'm working on it so any help is invaluable. Thank you for helping. 
      • Smitty Smith's avatar
        Smitty Smith
        Former Employee

        If your data is in a table, you can use Alt+H+D+L. If it's in a regular range, you can use Alt+E+D+R. Or the long way, go to Home > Cells > Delete >Delete sheet/table rows.

Resources