Home

How do I delete unused rows in a macro?

Highlighted
Philip Jen
Occasional Contributor

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.

@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. 

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.

Many thanks

I've actually managed to solve the problem in rather a complex way.

I have introduced an IF column of cells filled with IF(data_cell= "",9,1)

so when data is entered in the relevant data_cell the corresponding cell changes from 9 to 1.

 

Then further IF statements determine IF(cell=1,data,"") so now, all unused cells are blank.

When the Macro adds the cells to the master customer sheet, the  SORT ignores the blank cells.

 

Well, it might not be the best solution, but it works!

 

Thanks for your help -I'm learning !!

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies