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.
Here's the VBA Code I've been using to try and get the same results as the Formula in Excel:
Any help would be greatly appreciated. Thanks.
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | SAMPLE DATA | LOOKUP VALUES | RETURN VALUE | |||||||
2 | Name | Library | Card | Name | Library | Card | ||||
3 | Student1 | LAC | 8235-48756-7917 | Student4 | LAPL | 7778948142155 | v1 - Multi-Lookup Criteria | |||
4 | Student1 | LAPL | 7412-87380-0240 | 7778948142155 | v2 - Multi-Lookup Criteria | |||||
5 | Student2 | LAC | 1343-93958-0255 | |||||||
6 | Student2 | LAPL | 4087-11571-6391 | |||||||
7 | Student3 | LAC | 6232-19248-3050 | |||||||
8 | Student3 | LAPL | 4107-62169-7651 | |||||||
9 | Student4 | LAC | 3216-95560-5077 | |||||||
10 | Student4 | LAPL | 7778-94814-2155 | |||||||
11 | Student5 | LAC | 6102-11202-2237 | |||||||
12 | Student5 | LAPL | 2751-69146-4858 | |||||||
XlookupMulti |
Cell Formulas | ||
---|---|---|
Range | Formula | |
G3 | G3 | =XLOOKUP(1,(t_LibraryCards[Name]=$E$3)*(t_LibraryCards[Library]=$F$3),t_LibraryCards[Card]) |
G4 | G4 | =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.