Vba code for lookup if the value exists

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello all

I need an excel guru to solve this one please

I have multiple lookup's against a primary key in two work tabs

This then brings through the data I need

The problem I have is if the source line no longer exist the lookup will wipe out the last data that was in the cell..

I need something like if the primary key is not there stop the vlookup and leave the content in the cell, if the primary key exists then complete the lookup and over write the cell with the new data.

Code:
With Range("F1:F" & LastRow)
        .FormulaR1C1 = "=VLOOKUP(RC[-5],Data!C[-5]:C[15],21,FALSE)"
        .Value = .Value
        .Replace "#N/A", "", xlWhole
    End With
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try:
Code:
Sub M1()
    
    Dim x       As Long
    Dim dic     As Object
    Dim arr()   As Variant
    
    Application.ScreenUpdating = False
    
    With sheets("Data")
        arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 21).Value
    End With
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        dic(arr(x, 1)) = arr(x, 21)
    Next x
    
    With ActiveSheet
        arr = .Cells(1, 6).Resize(.Cells(.Rows.Count, 6).End(xlUp).Row).Value
        For x = LBound(arr, 1) To UBound(arr, 1)
            If dic.exists(arr(x, 1)) Then arr(x, 1) = dic(arr(x, 1))
        Next x
        
        .Cells(1, 6).Resize(UBound(arr, 1)).Value = arr
    End With
    
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
Hi JackDanIce

thank you for your reply

I ran your code but it's bugging out on this line

Code:
dic(arr(x, 1)) = arr(x, 21)

Run time error "Object Variable or With Block Variable not set"

If I debug and hover over "dic" then I get

Code:
[LEFT][COLOR=#222222][FONT=Tahoma]dic(arr(x, 1)) = [COLOR=#333333][FONT=Verdana]Object Variable or With Block Variable not set[/FONT][/COLOR][/FONT][/COLOR][/LEFT]
 
Last edited:
Upvote 0
Try:
Code:
Sub M1()
    
    Dim x       As Long
    Dim dic     As Object: Set dic = CreateObject("Scripting.Dictionary")
    Dim arr()   As Variant
    
    Application.ScreenUpdating = False
    
    With sheets("Data")
        arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 21).Value
    End With
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        dic(arr(x, 1)) = arr(x, 21)
    Next x
    
    With ActiveSheet
        arr = .Cells(1, 6).Resize(.Cells(.Rows.Count, 6).End(xlUp).Row).Value
        For x = LBound(arr, 1) To UBound(arr, 1)
            If dic.exists(arr(x, 1)) Then arr(x, 1) = dic(arr(x, 1))
        Next x
        
        .Cells(1, 6).Resize(UBound(arr, 1)).Value = arr
    End With
    
    Application.ScreenUpdating = True


    Set dic = Nothing
    Erase arr
    
End Sub
 
Upvote 0
Sorry to be a pain, the code runs now so I change the value in the "data" sheet on column 21 to see if the correct data would populate the activesheet with the same key in column 6 but it stays the same.
 
Upvote 0
Try this change
Code:
        arr = .Cells(1, [COLOR=#ff0000]1[/COLOR]).Resize(.Cells(.Rows.Count, [COLOR=#ff0000]1[/COLOR]).End(xlUp).Row).Value
 
Upvote 0
Hi Fluff

This is nearly correct, if the key in the active sheet column 1 exist in "Data" column 1 then it now puts this value into active sheet column 6 which is what I want
But if the key in the active sheet sheet column 1 does not exist in the "data" column1 then the active sheet has the column1 value entered into column 6 which overright the last value which I don't want
 
Upvote 0
Try
Code:
    With ActiveSheet
        arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 6).Value
        For x = LBound(arr, 1) To UBound(arr, 1)
            If dic.exists(arr(x, 1)) Then arr(x, 6) = dic(arr(x, 1))
        Next x
        
        .Cells(1, 6).Resize(UBound(arr)).Value = Application.Index(arr, 0, 6)
    End With
 
Upvote 0
That's it, spot on, thanks very much to both of you for helping me with this
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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