Excel VBA: Xlookup + Multi-Criteria + Copy to Windows Clipboard

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to get a VBA macro to COPY a value to the Windows Clipboard based on two Criteria.

I'm running a test using some sample data to match the Name and Library to a given Card number.

What I'm working with so far...
I've tested two different formula versions of using multi-criteria with xlookup and I get the correct return value.
However, after using these same two versions for VBA, neither are working for me.
VBA Testing.xlsm
ABCDEFGH
1SAMPLE DATALOOKUP VALUESRETURN VALUE
2NameLibraryCardNameLibraryCard
3Student1LAC8235-48756-7917Student4LAPL7778948142155v1 - Multi-Lookup Criteria
4Student1LAPL7412-87380-02407778948142155v2 - Multi-Lookup Criteria
5Student2LAC1343-93958-0255
6Student2LAPL4087-11571-6391
7Student3LAC6232-19248-3050
8Student3LAPL4107-62169-7651
9Student4LAC3216-95560-5077
10Student4LAPL7778-94814-2155
11Student5LAC6102-11202-2237
12Student5LAPL2751-69146-4858
XlookupMulti
Cell Formulas
RangeFormula
G3G3=XLOOKUP(1,(t_LibraryCards[Name]=$E$3)*(t_LibraryCards[Library]=$F$3),t_LibraryCards[Card])
G4G4=XLOOKUP($E$3&$F$3,t_LibraryCards[Name] & t_LibraryCards[Library],t_LibraryCards[Card])

Here's the VBA Code I've been using to try and get the same results as the Formula in Excel:

VBA Code:
'Copy Library Card Number
Sub Library_Card()
    
    'Data Types
    Dim CardName As String, CardLibrary As String, CardNum As String
    Dim LookupName As Range, LookupLibrary As Range, ReturnCard As Range
    
    'Table Ranges
    Set LookupName = Range("t_LibraryCards[Name]")
    Set LookupLibrary = Range("t_LibraryCards[Library]")
    Set ReturnCard = Range("t_LibraryCards[Card]")
    
    'Lookup Values
    CardName = Range("E3").Value
    CardLibrary = Range("F3").Value
    
    'Get Card Number
    
    'Using Single Criteria returns wrong match
    CardNum = Application.WorksheetFunction.Xlookup(CardName, LookupName, ReturnCard).Value
    
    'Using Multi-Criteria v1 returns Error: Type Mismatch
    CardNum = Application.WorksheetFunction.Xlookup(1, (LookupName = CardName) * (LookupLibrary = CardLibrary), ReturnCard).Value
    
    'Using Multi-Criteria v2 returns Error: Type Mismatch
'    CardNum = Application.WorksheetFunction.Xlookup(CardName & CardLibrary, LookupName & LookupLibrary, ReturnCard).Value

End Sub

Any help would be greatly appreciated. Thanks.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try it without .Value:
VBA Code:
    'Using Single Criteria returns wrong match
    CardNum = Application.WorksheetFunction.XLookup(CardName, LookupName, ReturnCard)
    
    'Using Multi-Criteria v1 returns Error: Type Mismatch
    CardNum = Application.WorksheetFunction.XLookup(1, (LookupName = CardName) * (LookupLibrary = CardLibrary), ReturnCard)
 
Upvote 0
I don't think you can join arrays/ranges like that. Try building the formula as a string and then evaluating it.

VBA Code:
Sub Library_Card()
    
    'Data Types
    Dim strFormula As String
    Dim CardName As String, CardLibrary As String, CardNum As String
    'Lookup Values
    CardName = Range("E3").Value
    CardLibrary = Range("F3").Value
    '
    ' XLookup Formula
    '
    strFormula = "xlookup(""" & CardName & CardLibrary & """, t_LibraryCards[Name] & t_LibraryCards[Library], t_LibraryCards[Card])"
    
    'Get Card Number
    CardNum = Evaluate(strFormula)
    MsgBox CardNum
End Sub
 
Upvote 0
Solution
Try it without .Value:
VBA Code:
    'Using Single Criteria returns wrong match
    CardNum = Application.WorksheetFunction.XLookup(CardName, LookupName, ReturnCard)
   
    'Using Multi-Criteria v1 returns Error: Type Mismatch
    CardNum = Application.WorksheetFunction.XLookup(1, (LookupName = CardName) * (LookupLibrary = CardLibrary), ReturnCard)
Hi Akuini,
Unfortunately, removing .value didn't resolve the issues. Turns out I was using the wrong logic to begin with.
I appreciate your time and effort; Thank you.
 
Upvote 0
I don't think you can join arrays/ranges like that. Try building the formula as a string and then evaluating it.

VBA Code:
Sub Library_Card()
  
    'Data Types
    Dim strFormula As String
    Dim CardName As String, CardLibrary As String, CardNum As String
    'Lookup Values
    CardName = Range("E3").Value
    CardLibrary = Range("F3").Value
    '
    ' XLookup Formula
    '
    strFormula = "xlookup(""" & CardName & CardLibrary & """, t_LibraryCards[Name] & t_LibraryCards[Library], t_LibraryCards[Card])"
  
    'Get Card Number
    CardNum = Evaluate(strFormula)
    MsgBox CardNum
End Sub
Hi myall_blues,
I ran your suggestion 'as is' and it returns the correct value.
However, I did run one minor change/experiment to combine two lines into one, and it too, worked. (y)

VBA Code:
CardNum = Evaluate("xlookup(""" & CardName & CardLibrary & """, t_LibraryCards[Name] & t_LibraryCards[Library], t_LibraryCards[Card])")
As for copying the Card Number to the Windows Clipboard, this one seems to be the simplest option I found a little earlier today:
VBA Code:
'Copy Card Number to Windows Clipboard
CreateObject("htmlfile").parentwindow.clipboarddata.setdata "text", CardNum
Although I've seen the Evaluate used in other cases, I can't say I fully understood when to use it and not. This will definitely be something I implement in some of my other more overly complicated macros.
Thanks again myall_blues, I don't think this is the first time you've helped me with my Excel VBA deficiencies. It's very much appreciated.
 
Upvote 0
I quite liked @Eric W's wording in a couple of posts: "If you use WorksheetFunction, then everything inside it must be native VBA code, NOT spreadsheet formula syntax."
Hi Alex,
I've taken that quote and saved it into my notes regarding the VBA xlookup function. This bit of information helps explain how I was going down the wrong path in the first place.
Thanks again…
 
Upvote 0
Great news and thanks for the feedback.
I ran your suggestion 'as is' and it returns the correct value.
However, I did run one minor change/experiment to combine two lines into one, and it too, worked. (y)

VBA Code:
CardNum = Evaluate("xlookup(""" & CardName & CardLibrary & """, t_LibraryCards[Name] & t_LibraryCards[Library], t_LibraryCards[Card])")
The reason I like to put it in a string first is because it can be quite tricky to get it right, especially when building complex formulas that need embedded quotes or a lot of variable substitution. While developing the code I can display the string using debug.print until it’s correct, prior to trying to use it as a formula.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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