VBA Vlookup only if cell in blank

VbaHell

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

I am trying to do the following

If the cell range "K" is blank complete a Vlookup
If the cell range "K" has data in do not perform a Vlookup so that the data remains in the cell, any help on this would be great please.

Code:
With Range("K1:K" & LastRow)
        .FormulaR1C1 = "=VLOOKUP(RC[-10],Data!C[-10]:C[3],14,FALSE)"
        .Value = .Value
    End With

This is some code that two of our Excel forum members helped me with for another issue which is similar but works on the cell in the sheet called "Data" being blank and I have tried to modify this

Code:
With Sheets("Data")
        arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 14).Value
    End With
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        dic(arr(x, 1)) = arr(x, 14)
    Next x
    
    With Sheets("Comments")
        arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 11).Value
        For x = LBound(arr, 1) To UBound(arr, 1)
            If dic.exists(arr(x, 1)) Then arr(x, 11) = dic(arr(x, 1))
        Next x
        
        .Cells(1, 11).Resize(UBound(arr)).Value = Application.Index(arr, 0, 11)
    End With
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Is this what you want?
Code:
With Sheets("Comments")
    arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 11).Value
    For x = LBound(arr, 1) To UBound(arr, 1)
        If arr(x, 11) = "" Then
           If dic.exists(arr(x, 1)) Then arr(x, 11) = dic(arr(x, 1))
        End If
    Next x
    
    .Cells(1, 11).Resize(UBound(arr)).Value = Application.Index(arr, 0, 11)
End With
 
Upvote 0
Hi Fluff

The vlookup bring in a date value, first run of this macro is good, when i run it again it reverses the dd/mm/yyyy to mm/dd/yyyy
I need it that if there is already a date then do not complete the vlookup
 
Upvote 0
If it pulls the dates correctly first time round I don't know why it's swapping to US dates when you re-run it.
 
Upvote 0
I only need the data could the code bring it over as text instead, this way I would not have an issue with a date
 
Upvote 0
Try
Code:
arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 11)[COLOR=#ff0000].Text[/COLOR]
 
Upvote 0
Sorry Fluff

I get a runtime error 13 "Type Mismatch" on

Code:
[LEFT][COLOR=#333333][FONT=monospace]arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 11)[/FONT][/COLOR][COLOR=#ff0000][COLOR=#ff0000][FONT=monospace].Text[/FONT][/COLOR][/COLOR][/LEFT]
 
Last edited:
Upvote 0
Must admit I wasn;t sure if that would work & couldn't test as Xl is a bit busy running something.

I'll have a look later
 
Upvote 0
How about
Code:
With Sheets("Index")
   arr = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 11).Value
   For x = LBound(arr, 1) To UBound(arr, 1)
      If dic.exists(arr(x, 1)) Then arr(x, 11) = CLng(dic(arr(x, 1)))
   Next x
   
  .Cells(1, 11).Resize(UBound(arr)).Value = Application.Index(arr, 0, 11)
   
End With
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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