VBA. Delete row in a range if Values in Column A do not equal Cell B2

MsPretzel

New Member
Joined
Jun 14, 2018
Messages
5
I am very rusty on VBA and need some help!
I need to delete rows within a range (rows 6 to 85) if the value in Column A does not equal B2.
I have pasted some example data below

[TABLE="class: grid, width: 433"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Currency[/TD]
[TD]GBP[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Employee[/TD]
[TD]Charlie[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Employee[/TD]
[TD]Event name[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Abi[/TD]
[TD]London[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Becky[/TD]
[TD]Singapore[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Charlie[/TD]
[TD]Paris[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Dave[/TD]
[TD]London[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD]Ed[/TD]
[TD]Singapore[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Charlie[/TD]
[TD]Paris[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Dave[/TD]
[TD]London[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Ed[/TD]
[TD]Singapore[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]Charlie[/TD]
[TD]Paris[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]Abi[/TD]
[TD]London[/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You could try something like...

Code:
Sub YourRoutine()
Dim Cnt As Integer


For Cnt = 85 To 6 Step -1
    If ActiveSheet.Range("A" & Cnt).Value <> ActiveSheet.Range("B2") Then
        ActiveSheet.Range(Cnt & ":" & Cnt).EntireRow.Delete
    End If
Next Cnt


End Sub
 
Upvote 0
Apologies, i may have been unclear about my second post... try using:

Code:
Sub YourRoutine()
Dim Cnt As Integer


For Cnt = 85 To 6 Step -1
     If ActiveSheet.Range("A" & Cnt).Value <> ActiveSheet.Range("B2").Value Then
          ActiveSheet.Rows(Cnt).EntireRow.Delete
     End If
Next Cnt


End Sub
 
Last edited:
Upvote 0
Another option
Code:
Sub Delrws()
   With Range("A6:A85")
      .Value = Evaluate(Replace("if(@<>$B$2,""=XXX"",@)", "@", .Address))
      .SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
   End With
End Sub
 
Upvote 0
Apologies, i may have been unclear about my second post... try using:

Code:
Sub YourRoutine()
Dim Cnt As Integer


For Cnt = 85 To 6 Step -1
     If ActiveSheet.Range("A" & Cnt).Value <> ActiveSheet.Range("B2").Value Then
          ActiveSheet.Rows(Cnt).EntireRow.Delete
     End If
Next Cnt


End Sub

Thanks, I used the following and it deleted all of the data up to row 6 - I think there must be something in the range line which I'm missing. I feel like it's really obvious and I just can't spot it! Should I be using xlup?

Sub DelUnreqRows()
Dim LRow As Integer


For LRow = 85 to 6 Step -1
If ActiveSheet.Range("A" & LRow).Value <> ActiveSheet.Range("B2") Then
ActiveSheet.Rows(LRow).EntireRow.Delete
End If
Next LRow


End Sub
 
Upvote 0
How odd, it worked for me. Is the string in cell B2 the same as some of the strings in column A (i'm just wondering if B2 has a space after the name "Charlie" or something similar)? Fluff can hopefully point us both in the right direction...
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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