VBA: referencing active cell row in a loop

bsb2013

New Member
Joined
Jul 16, 2010
Messages
31
I have a column with jobs, and I am trying to delete every row with job "A". However, I am not sure how to reference the active cell's row in the range function?

basically I'm trying something like this(not working):
If ActiveCell Like "A*" Then
Rows(ActiveCell.row:ActiveCell.row).Select
Selection.Delete Shift:=xlUp
end If
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try like this, which is for column A

Code:
Sub test4()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("A" & i).Value Like "A*" Then Rows(i).Delete
Next i
End Sub
 
Upvote 0
Try:
Code:
If ActiveCell Like "A*" Then
  ActiveCell.EntireRow.Delete Shift:=xlUp
End If
Though your question/post is implying you're actively selecting each cell in your column and then testing it if it contains A* and then deleting. You'll find it's not a good idea to use ".Select" alot in your code and it will speed up the execution of your code if you do not. May be an idea to post your full code...
 
Upvote 0
Try:
Code:
If ActiveCell Like "A*" Then
  ActiveCell.EntireRow.Delete Shift:=xlUp
End If
Though your question/post is implying you're actively selecting each cell in your column and then testing it if it contains A* and then deleting. You'll find it's not a good idea to use ".Select" alot in your code and it will speed up the execution of your code if you do not. May be an idea to post your full code...

That would seem logical but now I'm getting an "application-defined or object-defined error" run-time error 1004... any ideas of what could be causing this?
 
Upvote 0
Try this out, it is using Select and not to slow depending on how many rows it needs to check and clear out, but I do agree with JackDanIce would be better to see your code.
Sub tryme()
Range("B2").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value Like "*A*" Then
ActiveCell.EntireRow.Delete Shift:=xlUp
End If
Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top