Loop: If Vlookup <> cell value then clear cell.

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
758
Hello friends, hope all is well!

Please help me edit/fix the below code:
It is supposed to go through A2 & below till The cell is blank (Looping A2 to blank cell)

The code has a vlookup; I wish to have an IF statement: if vlookup result <> the cell value in B2 & below, then the clear contents of A2 & below.
if the lookup matches go to next cell of A.

Your kind help is needed and really appreciated in advance.

Code:
Sub test()
 Dim x As String
 Sheets("sheet1").Select
 Range("A2").Select
Do
If x = Application.WorksheetFunction.VLookup(ActiveCell.Value, Worksheets("sheet1").Range("J4:K11"), 2, False) <> ActiveCell.Offset(0, 1) Then
 ActiveCell.ClearConents
 Else
 
 ActiveCell.Value = x
 End If
 ActiveCell.Offset(1, 0).Activate
 Loop Until IsEmpty(ActiveCell.Value)
 End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Your if statement was a little confused. I haven't tested your syntax on your Vlookup but this corrects the if statement:

Code:
Sub test()
Dim x As String
Sheets("sheet1").Select
Range("A2").Select
    Do
        x = Application.WorksheetFunction.VLookup(ActiveCell.Value, Worksheets("sheet1").Range("J4:K11"), 2, False)
        If x <> ActiveCell.Offset(0, 1) Then
            ActiveCell.ClearConents
        Else
            ActiveCell.Value = x
        End If
        ActiveCell.Offset(1, 0).Activate
    Loop Until IsEmpty(ActiveCell.Value)
End Sub
 
Upvote 0
Hello friends, hope all is well!

Please help me edit/fix the below code:
It is supposed to go through A2 & below till The cell is blank (Looping A2 to blank cell)

The code has a vlookup; I wish to have an IF statement: if vlookup result <> the cell value in B2 & below, then the clear contents of A2 & below.
if the lookup matches go to next cell of A.

Your kind help is needed and really appreciated in advance.

Rich (BB code):
Sub test()
 Dim x As String
 Sheets("sheet1").Select
 Range("A2").Select
Do
If x = Application.WorksheetFunction.VLookup(ActiveCell.Value, Worksheets("sheet1").Range("J4:K11"), 2, False) <> ActiveCell.Offset(0, 1) Then
ActiveCell.ClearConents 'This is a mistake, Clearcontents is not an Activecell method
 Else
 
 ActiveCell.Value = x 'if the lookup matches go to next cell of A, Then you should not change the value of your cell.
 End If
 ActiveCell.Offset(1, 0).Activate
 Loop Until IsEmpty(ActiveCell.Value)
 End Sub


I wish to have an IF statement: if vlookup result <> B2, then clear contents of A2, if matches go to next cell of A.

According to the above, it could be like this:

Code:
Sub test1()
    Dim c As Range, f As Range
    Sheets("sheet1").Select
    For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
        Set f = Range("J4:K11").Find(c.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not f Is Nothing Then
            If f.Offset(1).Value <> c.Offset(1).Value Then c.ClearContents
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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