Range selection

M

m1ke

Hi all,

I found a macro on the net that does pretty much what I want, apar
from the fact that it only searches 1 column - not a range of column
as I would like.

Here is the code...


Code
-------------------
Sub Delete_Rows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("J:J"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "1" Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Su
-------------------


Basically, I require the macro to search through every cell in column
J to R, and when it finds a "1", delete the entire row. As you can see
the above works for just column J, but I'm not sure of the correc
syntax when trying to select a range of columns.

Any help appreciated
 
B

Bob Phillips

Just change the line

Set rng = Intersect(Range("J:J"), ActiveSheet.UsedRange)

to

Set rng = Intersect(Range("J:R"), ActiveSheet.UsedRange)


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
M

m1ke

Hi Bob,

Thanks for your reply.

Sorry, I should have mentioned that I did try what you suggested befor
I made the original post. However, it doesn't seem to work. It doesn'
give any error messages, but it doesn't remove 1's from any column.

Any ideas
 
D

Dave Peterson

One way:

Option Explicit
Sub Delete_Rows()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("J:R"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "1" Then
If del Is Nothing Then
Set del = cell.EntireRow.Cells(1)
Else
Set del = Union(del, cell.EntireRow.Cells(1))
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Refer to cell in diff sheet 1
Delete Rows help? 4
Macro deletes seperately 4
Delete Row Help 2
Excel VBA 1
VBA macro to delete rows that contain text 2
Object range failed 1
Fails to delete empty rows 4

Top