MACRO selecting rows that do not contain data in a particular row

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

JeffSchwartz

Thread Starter
Joined
Jan 31, 2013
Messages
56
In my quest to develop macros to manipulate our data I am trying to find a way to delete all rows that do not contain data in column J. Any ideas of how I could select rows without having the specific worksheet in the macro?

Whenever I try recording a macro using the sort function, it specifies the one worksheet where I recorded it.


This is what it recorded:
ActiveWorkbook.Worksheets("Z011_15A").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Z011_15A").Sort.SortFields.Add Key:=ActiveCell. _
Offset(-98, 0).Range("A1:A159"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Z011_15A").Sort
.SetRange ActiveCell.Offset(-99, -9).Range("A1:X160")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Keebellah

Trusted Advisor
Joined
Mar 27, 2008
Messages
6,611
First Name
Hans
Hi Jeff,
A loop that starts with the last row and back up, I'm typing if directly into the post so the syntax may need correcting here and there but this would be the general idea.

Make suer the active sheet is the one you want processed and I assume that A will be the column holding the max nr of rows with data, else you'll need another command to select the last filled cell and take that row.


Sum DeleteRows()
Dim lastrow as long
Dim xrow as long
lastrow = Range( "A" & Rows.Count).end(XlUp).row
for xrow = lastrow to 2 step -1
if len(trim(Range("J" & xrow).value)) = 0 then Range("J" & xrow).EntireRow.Delete
next xrow
End sub

What this macro does is tart with the last row, chek if J has data in it, if not it delets the row.

Happy coding :)

BTW You should edit the recorded Sort macro.
 
Joined
Jul 1, 2005
Messages
8,546
Hi Hans. :)

for xrow = lastrow to 2 step -1
I never got to grips with that "step" thing. Probably never will. :eek:

I am trying to find a way to delete all rows that do not contain data in column J ... without having the specific worksheet in the macro?
Hi OP. Hope this (which "acts on" the active worksheet) helps.

Sub test()
Columns("J:J").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 

Keebellah

Trusted Advisor
Joined
Mar 27, 2008
Messages
6,611
First Name
Hans
Hi Bomb, your code is faster, I am one that always forgets the SpecialCells()

The stpe -1 is to go bottoms up

Since the last row has been declared and you do it the other way around the lastrow will not comply and generate an error.

The step command is a handy one, but your code is much fastre (y)

I'll store it my folder witn hints and tips :)
 
Joined
Apr 17, 2012
Messages
455
2 problem with special cells.

1. if found nothing, will trigger error.
2. if found only one, will deem as single cell selection (this is a problem for find after special cells)

Sub test()
dim found as range
set found = Columns("J:J").SpecialCells(xlCellTypeBlanks)
if not found is nothing then
found.EntireRow.Delete
end if
End Sub
 
Joined
Jul 1, 2005
Messages
8,546
OK. How about:


Sub test()
LastRow = Range("A" & Rows.Count).End(xlUp).Row
If WorksheetFunction.CountA(Range("J1:J" & LastRow)) <> LastRow Then
Range("J1:J" & LastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If
End Sub


?
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top