vba vlookup and writing formula into cell

frostworks

New Member
Joined
Jan 28, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello! I have two tables here and am attempting to use vba to do two things:

1. lookup value in sheet 1 table 14 col 10, match against sheet 2 table 1 col 1, return sheet 2 table 1 col 6 value in sheet 1 table 14 col 60
2. write vlookup formula into sheet 2 table 1 col 6 to look up value in sheet 2 table 1 col 1 and return sheet 1 table 14 col 60

Can't seem to find a standardise/proper example online or maybe i'm too dense for it.. Can someone help me please?

Sheet 1 Table 14
1710383547344.png


Sheet 2 Table 1
1710383623053.png


Thanks in advance!
 
Struggling to fully comprehend without sample data/formulas but see if this is it.

VBA Code:
Sub Test3()
  Dim d As Object
  Dim T14_10 As Variant, T14_60 As Variant, T1_1 As Variant, T1_6 As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  T14_10 = Range("Table14[col 10]").Value
  T14_60 = Range("Table14[col 60]").Value
  T1_1 = Range("Table1[col 1]").Value
  T1_6 = Range("Table1[col 6]").Value
  For i = 1 To UBound(T1_1)
    If Len(T1_6(i, 1)) > 0 Then d(T1_1(i, 1)) = T1_6(i, 1)
  Next i
  For i = 1 To UBound(T14_10)
    If d.exists(T14_10(i, 1)) Then T14_60(i, 1) = d(T14_10(i, 1))
  Next i
  Range("Table14[col 60]").Value = T14_60
  With Range("Table1[col 6]")
    .ClearContents
    .Cells(1).Formula2 = "=XLOOKUP([@[col 1]],Table14[col 10],Table14[col 60])&"""""
  End With
End Sub

what's the '&""""" ' portion for?
So that if thee looked up cell is empty the formula returns "" rather than 0
 
Upvote 0
Solution

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Struggling to fully comprehend without sample data/formulas but see if this is it.

VBA Code:
Sub Test3()
  Dim d As Object
  Dim T14_10 As Variant, T14_60 As Variant, T1_1 As Variant, T1_6 As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  T14_10 = Range("Table14[col 10]").Value
  T14_60 = Range("Table14[col 60]").Value
  T1_1 = Range("Table1[col 1]").Value
  T1_6 = Range("Table1[col 6]").Value
  For i = 1 To UBound(T1_1)
    If Len(T1_6(i, 1)) > 0 Then d(T1_1(i, 1)) = T1_6(i, 1)
  Next i
  For i = 1 To UBound(T14_10)
    If d.exists(T14_10(i, 1)) Then T14_60(i, 1) = d(T14_10(i, 1))
  Next i
  Range("Table14[col 60]").Value = T14_60
  With Range("Table1[col 6]")
    .ClearContents
    .Cells(1).Formula2 = "=XLOOKUP([@[col 1]],Table14[col 10],Table14[col 60])&"""""
  End With
End Sub


So that if thee looked up cell is empty the formula returns "" rather than 0
This works, excellent thanks!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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