VBA deleting rows based on a particular value

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359
Excel 2002

On Sheet1 I have a value in cell A15. On Sheet2 I have values in column B:B. The values in the B column are actually repeated for 16 rows before the value changes and then is repeated for another 16 rows.

What I want to be able to do is create a macro that will look at the value in cell A15 Sheet1 and then go to Sheet2 and delete the rows in which the value is also seen in the B column.

I have the following code which will delete rows when the A column is blank:

Dim i As Long
For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
If Cells(i, 1).Value = "" Then Cells(i, 1).EntireRow.Delete
Next i

And I tried to amend it to do what I wanted but it didnt work. I wont post my amendment since it didnt work, but I could if it was needed.

Any help would be greatly appreciated. Thank you!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try

Code:
Dim i As Long
With Sheets("Sheet2")
    For i = .Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    If .Cells(i, 2).Value = Sheets("Sheet1").Range("A15").Value Then .Rows(i).Delete
    Next i
End With
 
Upvote 0
VoG,

I tried your code, but nothing happened. I didnt get a debug or anything, just nothing happened. Right now I have the code in a module. Is that ok?
 
Upvote 0
The code should go in a regular module.

Should the code be looking for an exact match with A15 or cells that contain A15's value.

Note that I edited my code a couple of times after posting originally so you might want to check that you have the version that appears now.
 
Upvote 0
Yes, I do have that code, I re-pasted it, just to make sure.

"Should the code be looking for an exact match with A15 or cells that contain A15's value."

That seems to be the same question to me, so I think I must not be explaining it correctly, sorry. The code should be looking for an exact Match with A15.

So for example, in cell A15, Sheet1 I have "11-11925". On Sheet2, in the B column (A column is empty), in cells B2:B17 I have "11-11924" and then in B18:B33 I have "11-11925". Since that value exactly matches what is in cell A15 on sheet one, I would want rows 18:33 deleted, on sheet2.

I hope I explained it better. Thank you for your time
 
Upvote 0
It worked for me when I changed

For i = .Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1

to

For i = .Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
 
Upvote 0
It isn't doing anything because column A is empty. try

Rich (BB code):
Dim i As Long
With Sheets("Sheet2")
    For i = .Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
    If .Cells(i, 2).Value = Sheets("Sheet1").Range("A15").Value Then .Rows(i).Delete
    Next i
End With
 
Upvote 0
You are welcome. Please always state things like columns (esp. A) being empty :)
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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