Hi,
I'm having trouble retrieving data from an external workbook using Xlookup in VBA. I can get the code to work with a worksheet in the same workbook, but not another workbook. The error message is 1004 Application-defined or object-defined error and its showing on the .formula line.
I'm trying to import the related data from the "TbClaims" table using the reference numbers in column A of both sheets. The code is still incomplete as I'll need to add the data from the other related columns from "TbClaims".
I would like to use column headers as lookup values because the column positions may change. Also if there are any suggestions as to changing other parts of the code to reference the column headers, it would be very much appreciated.
I'm very new to VBA. Here is the code:
I'm having trouble retrieving data from an external workbook using Xlookup in VBA. I can get the code to work with a worksheet in the same workbook, but not another workbook. The error message is 1004 Application-defined or object-defined error and its showing on the .formula line.
I'm trying to import the related data from the "TbClaims" table using the reference numbers in column A of both sheets. The code is still incomplete as I'll need to add the data from the other related columns from "TbClaims".
I would like to use column headers as lookup values because the column positions may change. Also if there are any suggestions as to changing other parts of the code to reference the column headers, it would be very much appreciated.
I'm very new to VBA. Here is the code:
VBA Code:
Sub IMPORT_DATA_CLAIMS()
Dim ewb As Workbook, twb As Worksheet, ews As Worksheet
Dim lr As Long, i As Long
Dim TableName As ListObject
Set twb = ThisWorkbook.Sheets("Minutes")
Set ewb = Workbooks.Open("Excel - Claim reg demo.xlsx")
Set ews = ewb.Worksheets("Claims")
Set TableName = ews.ListObjects("TbClaims")
lr = twb.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lr
If Range("G" & i).Value = "" Then
With Range("J" & i)
.Formula = "=XLOOKUP([@Ref],TableName[Reference],TableName[Amount Granted],"""")"
.Value = .Value
End With
End If
Next i
ewb.Close
End Sub