Convert formula into vba macro

eddorena

New Member
Joined
Dec 3, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Please help me to convert this formula by using a VBA macro, this formula helps me to auto Xlookup for entire line (without putting same formula in another line)

Excel Formula:
=FILTER(XLOOKUP(ISGPN,'[FILEBAPREF.xlsx]Sheet1'!$B:$B,'[FILEBAPREF.xlsx]Sheet1'!$G:$G,"",0),XLOOKUP(A:A,'[FILEBAPREF.xlsx]Sheet1'!$B:$B,'[FILEBAPREF.xlsx]Sheet1'!$G:$G,"",0)<>"")

I try this one and not working well.

VBA Code:
result.Value = Application.WorksheetFunction.Filter(formula, Evaluate(formula.Address & "<>"""""))

The formula name above is code as below

VBA Code:
formula.Value = Application.WorksheetFunction.XLookup(Lookuplist, wb.Worksheets("PNR").Range("B:B"), wb.Worksheets("PNR").Range("G:G"), """", 0)

Thank You
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Here is a VBA macro that performs the same Xlookup function as your formula and populates the results in the same row as the input values:

VBA Code:
Sub XlookupMacro()
    Dim lastRow As Long
    lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 'get the last row with data in column A
    Dim lookupRange As Range
    Set lookupRange = Range("B1:B100") 'set the lookup range as required
    Dim lookupResult As Variant
    Dim i As Long
    For i = 1 To lastRow
        If Not IsEmpty(Cells(i, "A")) Then 'check if cell in column A is not empty
            lookupResult = Application.XLookup(Cells(i, "A"), lookupRange, Range("G1:G100"), "", 0, 1) 'perform Xlookup
            lookupResult = WorksheetFunction.Filter(lookupResult, WorksheetFunction.XLookup(Range("A" & i), lookupRange, Range("G1:G100"), "", 0, 1) <> "") 'filter lookup result
            Range("H" & i).Resize(1, UBound(lookupResult)) = lookupResult 'populate result in row i, starting from column H
        End If
    Next i
End Sub

You can modify the range lookupRange to match the range of your lookup values in column B, and adjust the range G1:G100 to match the range of your return values. The macro loops through each row in column A and performs the Xlookup and Filter functions on the corresponding row in column B. The results are then populated in the same row as the input value, starting from column H.
 
Upvote 0
Here is a VBA macro that performs the same Xlookup function as your formula and populates the results in the same row as the input values:

VBA Code:
Sub XlookupMacro()
    Dim lastRow As Long
    lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 'get the last row with data in column A
    Dim lookupRange As Range
    Set lookupRange = Range("B1:B100") 'set the lookup range as required
    Dim lookupResult As Variant
    Dim i As Long
    For i = 1 To lastRow
        If Not IsEmpty(Cells(i, "A")) Then 'check if cell in column A is not empty
            lookupResult = Application.XLookup(Cells(i, "A"), lookupRange, Range("G1:G100"), "", 0, 1) 'perform Xlookup
            lookupResult = WorksheetFunction.Filter(lookupResult, WorksheetFunction.XLookup(Range("A" & i), lookupRange, Range("G1:G100"), "", 0, 1) <> "") 'filter lookup result
            Range("H" & i).Resize(1, UBound(lookupResult)) = lookupResult 'populate result in row i, starting from column H
        End If
    Next i
End Sub

You can modify the range lookupRange to match the range of your lookup values in column B, and adjust the range G1:G100 to match the range of your return values. The macro loops through each row in column A and performs the Xlookup and Filter functions on the corresponding row in column B. The results are then populated in the same row as the input value, starting from column H.
Hi Learnerz, I'll testing right now.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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