+ Reply to Thread
Results 1 to 14 of 14

How to use XLookup on VBA by searching 2 criteria/Condition

  1. #1
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    How to use XLookup on VBA by searching 2 criteria/Condition

    Hi all, I'm having some issues on converting the excel formula code into VBA

    =XLOOKUP(G7&G9,MasterList!A:A&MasterList!B:B,MasterList!D:D,"NotFound") <-- Working formula used in excel sheet

    Meanwhile if I were to use single value search, I'm able to execute it but it's not accurate enough for me as I'll need to determine two content to have the final return value back to me.

    'Range("G13").Value = Application.WorksheetFunction.XLookup(Range("G7"), Sheets("MasterList").Range("A:A"), Sheets("MasterList").Range("B:B")) <-- Working single content search

    I had tried the following VBA code but It keep showing me range-object error.
    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7" & "G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"))


    Any help is appreciated, many thanks!
    Last edited by EssperTan; 01-01-2024 at 09:11 PM.

  2. #2
    Forum Contributor
    Join Date
    02-04-2017
    Location
    chennai
    MS-Off Ver
    MS OFFICE 365
    Posts
    253

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Dear EssperTan
    It looks like you're trying to concatenate the values in cells G7 and G9 and use them as a combined lookup value in VBA. However, the syntax for concatenation is incorrect in your code.

    Here's the corrected version of your VBA code:
    Please Login or Register  to view this content.
    I use Range("G7").Value & Range("G9").Value to concatenate the values in cells G7 and G9.
    I use .Value to access the values of the ranges in MasterList.
    I also include the "NotFound" argument at the end of the XLookup function, similar to your original Excel formula.
    Make sure that the ranges you are working with contain valid data, and the concatenated value from G7 and G9 is found in the combined ranges A:A and B:B in MasterList. If the concatenated value is not found, the XLookup function will return the specified "NotFound" value.

  3. #3
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Hi Sudbhavani,

    Thanks for the reply!
    I had tried using your code, but I'm receiving some mismatch error.

    Let me give a rough idea of what I want to achieve too.
    So basically, I'm trying to make an user interface whereby the quantity balance will be deducted base on the name & manufacturer. Hence that's why I need to use XLookup for better accuracy as it can find 2 value and return the balance value back.

    Do have a look at my GUI & the code below for better understanding, thanks for all the help given!
    Attachment 853111



    <----------Here is my full list of VBA code-------------------->
    Private Sub ComponentAvailbility_Click()

    Dim Check As String
    Dim Number As String

    Check = Range("J7")
    Number = Range("J8")
    If Check = "Yes" Then
    MsgBox "Item is available"
    Else
    MsgBox "Item is not available in the store"
    End If

    End Sub

    Private Sub Execute_Click()

    Dim CellValue As String
    Dim ValueCheck As String
    Dim ItemAvailable As String
    Dim QtySubt As String
    Dim NameCheck As String
    Dim Value1 As Integer, Value2 As Integer, EnoughValue As Integer

    ValueCheck = Range("J8") 'Checking for value keyed in (number or text)
    ItemAvailable = Range("J7") 'Checking for item availability
    NameCheck = Range("H6") 'Checking for username
    If ValueCheck = "True" Then
    If ItemAvailable = "Yes" Then
    Value1 = Range("G11").Value
    Value2 = Range("G10").Value

    If NameCheck = "True" Then
    EnoughValue = Value1 - Value2 'Ensure enough stock in store
    If EnoughValue >= 0 Then

    CellValue = Range("G12")


    ''============Vlookup code program section=====================================================================================

    Dim longRow As Long
    Dim longRow2 As Long

    On Error Resume Next
    longRow = Application.WorksheetFunction.Match(Worksheets("GUI").Range("G7"), Worksheets("MasterList").Range("A:A"), 0)
    longRow2 = Application.WorksheetFunction.Match(Worksheets("GUI").Range("G9"), Worksheets("MasterList").Range("B:B"), 0)
    If Err.Number <> 0 Then
    MsgBox " Search failed"
    Exit Sub
    End If
    On Error GoTo 0
    Worksheets("MasterList").Range("A:A").Cells(longRow, 4) = Sheets("GUI").Range("G12")

    ''================================Checking XLoopup to replace Vlookup code program==============================================

    'Range("G13").Value = Application.WorksheetFunction.XLookup(Range("G7"), Sheets("MasterList").Range("A:A"), Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D")) 'working but single lookup

    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7" & "G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"))

    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7") & Range("G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"), "NotFound")
    Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7").Value & Range("G9").Value, Sheets("MasterList").Range("A:A").Value & Sheets("MasterList").Range("B:B").Value, Sheets("MasterList").Range("D:D").Value, "NotFound")

    '========================================================================================================

    MsgBox "The remaining Qty is " & CellValue


    Else

    MsgBox "Insufficient Quantity in store"

    End If
    Else
    MsgBox "Please enter a valid name!"
    End If
    Else
    MsgBox "Please enter an valid component"
    End If
    Else
    MsgBox "Please insert a numeric number!"
    End If


    End Sub
    <----------------------------------------------------------->

  4. #4
    Forum Contributor
    Join Date
    02-04-2017
    Location
    chennai
    MS-Off Ver
    MS OFFICE 365
    Posts
    253

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    It looks like there are a few issues in your code. I'll provide some corrections and explanations to help you understand the changes. I've modified the relevant part of your code where you use XLookup:

    Please Login or Register  to view this content.

    Explanation of changes:

    I introduced a result variable to store the result of the XLookup function.
    I removed the concatenation of Range("G7").Value & Range("G9").Value inside the XLookup function, as XLookup can handle multiple criteria without concatenation.
    I added error handling to check if the XLookup was successful. If successful, it updates CellValue with the result; otherwise, it displays an error message.
    Make sure to adapt this code to fit the structure of your workbook and the logic of your application.

  5. #5
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    I had implemented the code in, now I am able to compile the program but I'm unable to retrieve any value from it as shown in the attached picture.
    and the value does not
    May I know where did I put wrong?

    Thank you for all the help given!

    Attachment 853224 <- no value Attachment 853228 <- got value for checking
    ---------- Here is the updated code --------------

    Private Sub Execute_Click()

    Dim CellValue As String
    Dim ValueCheck As String
    Dim ItemAvailable As String
    Dim QtySubt As String
    Dim NameCheck As String
    Dim Value1 As Integer, Value2 As Integer, EnoughValue As Integer

    ValueCheck = Range("J8") 'Checking for value keyed in (number or text)
    ItemAvailable = Range("J7") 'Checking for item availability
    NameCheck = Range("H6") 'Checking for username
    If ValueCheck = "True" Then
    If ItemAvailable = "Yes" Then
    Value1 = Range("G11").Value
    Value2 = Range("G10").Value

    If NameCheck = "True" Then
    EnoughValue = Value1 - Value2 'Ensure enough stock in store
    If EnoughValue >= 0 Then

    CellValue = Range("G12")
    MsgBox "The cellvalue Qty is " & CellValue 'Just to check the value (to be remove after everything works)

    ''============Vlookup code program section=====================================================================================

    'Dim longRow As Long
    'Dim longRow2 As Long
    '
    'On Error Resume Next
    'longRow = Application.WorksheetFunction.Match(Worksheets("GUI").Range("G7"), Worksheets("MasterList").Range("A:A"), 0)
    'longRow2 = Application.WorksheetFunction.Match(Worksheets("GUI").Range("G9"), Worksheets("MasterList").Range("B:B"), 0)
    'If Err.Number <> 0 Then
    'MsgBox " Search failed"
    'Exit Sub
    'End If
    'On Error GoTo 0
    'Worksheets("MasterList").Range("A:A").Cells(longRow, 4) = Sheets("GUI").Range("G12")

    ''============XLookup code program section=====================================================================================

    Dim result As Variant

    On Error Resume Next
    result = Application.WorksheetFunction.XLookup(Range("G7").Value & Range("G9").Value, Sheets("MasterList").Range("A:A").Value & Sheets("MasterList").Range("B:B").Value, Sheets("MasterList").Range("D:D").Value, "NotFound")
    On Error GoTo 0

    If Not IsError(result) Then
    ' Value found, update the balance
    CellValue = result
    MsgBox "The remaining Qty is " & CellValue
    Else
    ' Value not found
    MsgBox "Search failed"
    End If

    ''========================================================================================================



    ''================================Checking XLoopup to replace Vlookup code program==============================================

    'Range("G13").Value = Application.WorksheetFunction.XLookup(Range("G7"), Sheets("MasterList").Range("A:A"), Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D")) 'working but single lookup

    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7" & "G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"))

    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7") & Range("G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"), "NotFound")
    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7").Value & Range("G9").Value, Sheets("MasterList").Range("A:A").Value & Sheets("MasterList").Range("B:B").Value, Sheets("MasterList").Range("D:D").Value, "NotFound")

    '========================================================================================================

    'MsgBox "The remaining Qty is " & CellValue


    Else

    MsgBox "Insufficient Quantity in store"

    End If
    Else
    MsgBox "Please enter a valid name!"
    End If
    Else
    MsgBox "Please enter an valid component"
    End If
    Else
    MsgBox "Please insert a numeric number!"
    End If


    End Sub

  6. #6
    Forum Contributor
    Join Date
    02-04-2017
    Location
    chennai
    MS-Off Ver
    MS OFFICE 365
    Posts
    253

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Quote Originally Posted by EssperTan View Post
    I had implemented the code in, now I am able to compile the program but I'm unable to retrieve any value from it as shown in the attached picture.
    and the value does not
    May I know where did I put wrong?

    Thank you for all the help given!

    Attachment 853224 <- no value Attachment 853228 <- got value for checking
    ---------- Here is the updated code --------------

    Private Sub Execute_Click()

    Dim CellValue As String
    Dim ValueCheck As String
    Dim ItemAvailable As String
    Dim QtySubt As String
    Dim NameCheck As String
    Dim Value1 As Integer, Value2 As Integer, EnoughValue As Integer

    ValueCheck = Range("J8") 'Checking for value keyed in (number or text)
    ItemAvailable = Range("J7") 'Checking for item availability
    NameCheck = Range("H6") 'Checking for username
    If ValueCheck = "True" Then
    If ItemAvailable = "Yes" Then
    Value1 = Range("G11").Value
    Value2 = Range("G10").Value

    If NameCheck = "True" Then
    EnoughValue = Value1 - Value2 'Ensure enough stock in store
    If EnoughValue >= 0 Then

    CellValue = Range("G12")
    MsgBox "The cellvalue Qty is " & CellValue 'Just to check the value (to be remove after everything works)

    ''============Vlookup code program section=====================================================================================

    'Dim longRow As Long
    'Dim longRow2 As Long
    '
    'On Error Resume Next
    'longRow = Application.WorksheetFunction.Match(Worksheets("GUI").Range("G7"), Worksheets("MasterList").Range("A:A"), 0)
    'longRow2 = Application.WorksheetFunction.Match(Worksheets("GUI").Range("G9"), Worksheets("MasterList").Range("B:B"), 0)
    'If Err.Number <> 0 Then
    'MsgBox " Search failed"
    'Exit Sub
    'End If
    'On Error GoTo 0
    'Worksheets("MasterList").Range("A:A").Cells(longRow, 4) = Sheets("GUI").Range("G12")

    ''============XLookup code program section=====================================================================================

    Dim result As Variant

    On Error Resume Next
    result = Application.WorksheetFunction.XLookup(Range("G7").Value & Range("G9").Value, Sheets("MasterList").Range("A:A").Value & Sheets("MasterList").Range("B:B").Value, Sheets("MasterList").Range("D:D").Value, "NotFound")
    On Error GoTo 0

    If Not IsError(result) Then
    ' Value found, update the balance
    CellValue = result
    MsgBox "The remaining Qty is " & CellValue
    Else
    ' Value not found
    MsgBox "Search failed"
    End If

    ''========================================================================================================



    ''================================Checking XLoopup to replace Vlookup code program==============================================

    'Range("G13").Value = Application.WorksheetFunction.XLookup(Range("G7"), Sheets("MasterList").Range("A:A"), Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D")) 'working but single lookup

    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7" & "G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"))

    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7") & Range("G9"), Sheets("MasterList").Range("A:A") & Sheets("MasterList").Range("B:B"), Sheets("MasterList").Range("D:D"), "NotFound")
    'Range("G14").Value = Application.WorksheetFunction.XLookup(Range("G7").Value & Range("G9").Value, Sheets("MasterList").Range("A:A").Value & Sheets("MasterList").Range("B:B").Value, Sheets("MasterList").Range("D:D").Value, "NotFound")

    '========================================================================================================

    'MsgBox "The remaining Qty is " & CellValue


    Else

    MsgBox "Insufficient Quantity in store"

    End If
    Else
    MsgBox "Please enter a valid name!"
    End If
    Else
    MsgBox "Please enter an valid component"
    End If
    Else
    MsgBox "Please insert a numeric number!"
    End If


    End Sub
    what error message your getting i not able open your attachment so please share the error messaged. so i able to correct the code

  7. #7
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Hi, when I run the code. I'm getting a blank value in return. Attachment 853242

  8. #8
    Forum Contributor
    Join Date
    02-04-2017
    Location
    chennai
    MS-Off Ver
    MS OFFICE 365
    Posts
    253

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Dear EssperTan

    i not able to open the attached i will explain the process to attached file: GoAdvanend tab and select the manage tab and attached the file and submits the post

  9. #9
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Ok, here you go. Are you able to see it now?
    GUI2.PNG

  10. #10
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Here is the excel file too if you need it. I think it will be better too so you can see the whole process.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    02-04-2017
    Location
    chennai
    MS-Off Ver
    MS OFFICE 365
    Posts
    253

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Dear EssperTan

    please check the attachment i thank now code is ruining successfully is there any issue let me no what the issue your getting

    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    You can never evaluate Runtime error by IsError function, but Err.Number.

    Try change that block to something like
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    12-19-2023
    Location
    Singapore
    MS-Off Ver
    2020
    Posts
    7

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    Thanks all for the help and knowledge given! the issues is finally resolved!

  14. #14
    Forum Contributor
    Join Date
    02-04-2017
    Location
    chennai
    MS-Off Ver
    MS OFFICE 365
    Posts
    253

    Re: How to use XLookup on VBA by searching 2 criteria/Condition

    dear EssperTa
    kindly mark add reputation and mark as solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Xlookup horizontally 3 criteria and vertically 1 criteria, can it be done?
    By oneblondebrow in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-20-2023, 10:53 AM
  2. How to use XLOOKUP with a max Date criteria
    By punksterz626 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-29-2023, 12:53 PM
  3. Xlookup multiple criteria
    By mcis19 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-26-2023, 11:18 AM
  4. Xlookup functions with 2 criteria
    By kirana2014 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-22-2023, 09:26 PM
  5. Using XLOOKUP for 2 search criteria
    By santoma in forum Excel General
    Replies: 10
    Last Post: 01-13-2023, 03:12 PM
  6. Xlookup with condition
    By sashaxiv in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-19-2022, 08:23 AM
  7. XLOOKUP searching in two places for the same criteria?
    By Msilvester in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-22-2021, 12:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1