VBA VLookup to replace certain values only

lopiteaux

Board Regular
Joined
Jun 8, 2011
Messages
77
Hi all - I'm a bit stuck with a fairly basic VLookup and was hoping someone could help. In short, what I want the VLookup to do is go down a list of values in column P, perform a VLookup using the primary key in column A on a seperate sheet in the workbook, and replace ONLY those values for which it finds the key.

The code I have at the moment returns the value if found and populates with "TEST IGNORE" if not found... I cannot seem to get the code to just skip to the next cell WITHOUT changing the value in the active cell...

Code:
Sub UpdateValues()
    
    Range("P1").Select
      
    Do
        ActiveCell.Value = Application.VLookup(ActiveCell.Offset(0, -15), Sheets("LOOKUPSOURCE").Columns("A:F"), 6, False)
        If IsError(ActiveCell.Value) = True Then
            ActiveCell.Value = "TEST IGNORE"
            ActiveCell.Offset(1, 0).Select
        Else
            ActiveCell.Value = Application.VLookup(ActiveCell.Offset(0, -15), Sheets("LOOKUPSOURCE").Columns("A:F"), 6, False)
            ActiveCell.Offset(1, 0).Select
        End If
    Loop Until IsEmpty(ActiveCell.Offset(0, -15).Value)
    
End Sub

Any ideas?

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think this code below is what you want, if not tell us.

Code:
Sub UpdateValues()
    'Definition of variables of the macro
    Dim myCell As Range
    Dim myLookup
 
    'Navigates by cells of the column P until the corresponding
    'cell (in the same row) of the column is empty
    For Each myCell In Sheets("VLOOKUP").Range("P:P")
        'If the corresponding cell in column A is empty Exit For
        If IsEmpty(myCell.Offset(0, -15).Value) Then
            Exit For
        End If
        'Ignores errors encountered
        On Error Resume Next
        'Stores the result of the Vlookup function in
        'the variable myLookup
        myLookup = WorksheetFunction.VLookup(myCell.Offset(0, -15), _
            Sheets("LOOKUPSOURCE").Columns("A:F"), 6, False)
        'If no error occurred running the Vlookup function
        'Stores the value found in the corresponding cell in column P
        If Err = 0 Then
            myCell.Value = myLookup
        Else
            'If an error occurs clears all property settings of the
            'Err object for the next myCell
            Err.Clear
        End If
        'Active error checking
        On Error GoTo 0
    Next myCell
End Sub

Markmzz
 
Upvote 0
Take full advantage of VBA's Powers...

Use Variables to store values, instead of putting them in a cell.
This way, you only do the vlookup once (not twice)
Assign it's result to a variable.
Then you test the variable for error
If not an error, put the value of the variable in the cell.

Try this

Code:
Sub UpdateValues()
Dim lr As Long, i As Long
Dim x As Variant
lr = Range("A1").End(xlDown).Row
For i = 1 to lr
    x = Application.Vlookup(Cells(i,"A"),Sheets("LOOKUPSOURCE").Columns("A:F"), 6, False)
    If Not(Iserror(x)) Then
        Cells(i,"P").Value = x
    End If
Next i
End Sub
 
Last edited:
Upvote 0
Another solution:

Code:
Sub UpdateValuesV2()
    'Definition of variables of the macro
    Dim myCell As Range
    Dim myLookup
 
    'Navigates by cells of the column P until the corresponding
    'cell (in the same row) of the column is empty
    For Each myCell In Sheets("VLOOKUP").Range("P:P")
        'If the corresponding cell in column A is empty Exit For
        If IsEmpty(myCell.Offset(0, -15).Value) Then
            Exit For
        End If
        'Stores the result of the Vlookup function in
        'the variable myLookup
        myLookup = Application.VLookup(myCell.Offset(0, -15), _
            Sheets("LOOKUPSOURCE").Columns("A:F"), 6, False)
        'If no error occurred running the Vlookup function
        'Stores the value found in the corresponding cell in column P
        If Not IsError(myLookup) Then
            myCell.Value = myLookup
        End If
    Next myCell
End Sub
 
Upvote 0
jonmo1, you're a genius. works like a charm, and clean as well! i'll remember this one...

thanks for all the help guys

l
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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