SOLVED

VBA code - XLookup

%3CLINGO-SUB%20id%3D%22lingo-sub-1864658%22%20slang%3D%22en-US%22%3EVBA%20code%20-%20XLookup%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1864658%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%20TY%20in%20advance%20for%20helping%20me.%26nbsp%3B%20I%20am%20learning%20to%20code%20VBA%20and%20looking%20for%20a%20VBA%20solution...I%20understand%20Xlookup%20in%20regular%20Excel.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20included%20a%20data%20sample%20and%20the%20code.%26nbsp%3B%20The%20first%20half%20of%20the%20code%20works%20fine%20and%20retrieves%20the%20min%20or%20max%20value%20as%20asked%20for%20column%20H.%26nbsp%3B%20But%20my%20Xlookup%20isn't%20working%20for%20the%20three%20highlighted%20cells%20in%20column%20G.%26nbsp%3B%20I've%20googled%20my%20little%20heart%20out%20but%20can't%20figure%20out%20how%20to%20make%20it%20find%20the%20values%20in%20column%20H%20to%20retrieve%20the%20item%20name%20associated%20with%20it.%26nbsp%3B%20I've%20tried%20Cells(2%2C8)%20and%20I've%20tried%20MaxValue2.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20added%20the%20file%20and%20a%20screenshot%20of%20it.%26nbsp%3B%20Any%20thoughts%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22CodeQ.PNG%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232177i211DD1291B88A410%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22CodeQ.PNG%22%20alt%3D%22CodeQ.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1864658%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1864865%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20-%20XLookup%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1864865%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F861350%22%20target%3D%22_blank%22%3E%40Danak995%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20line%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-basic%22%3E%3CCODE%3ERange(%22G2%22)%20%3D%20WorksheetFunction.XLookup(Cells(2%2C%208)%2C%20%22C2%3AC8%22%2C%20%22A2%3AA8%22%2C%20%22Not%20found%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3Bhas%20strings%20as%20parameters%20rather%20than%20range%20references%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-basic%22%3E%3CCODE%3ERange(%22G2%22)%20%3D%20WorksheetFunction.XLookup(%5BH8%5D%2C%20%5BC2%3AC8%5D%2C%20%5BA2%3AA8%5D%2C%20%22Not%20found%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Ewhere%20%5BA2%3AA8%5D%20is%20an%20alternative%20notation%20for%20Range(%22A2%3AA8%22).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOther%20comments%20are%20that%20it%20is%20good%20practice%20to%20use%20Option%20Explicit%20to%20force%20the%20declaration%20of%20variables%3B%20I%20prefer%20the%20use%20of%20Defined%20Names%20or%20Tables%20(List%20Objects)%20to%20the%20direct%20referencing%20to%20avoid%20problems%20when%20rows%20are%20added%20or%20deleted%20to%20the%20sheet.%3C%2FP%3E%3CP%3EGiven%20the%20choice%2C%20I%20would%20use%20worksheet%20functions%20directly%20within%20the%20grid%20rather%20than%20from%20VBA%2C%20but%20I%20appreciate%20this%20is%20a%20learning%20exercise.%26nbsp%3B%20Some%20thing%20that%20may%20be%20of%20interest%20is%20that%20it%20is%20possible%20to%20write%20the%20worksheet%20formulas%20from%20VBA.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3ERange(%22G2%22).Formula%20%3D%20%22%3D%20XLookup(H2%2C%20C2%3AC8%2C%20A2%3AA8)%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1864942%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20-%20XLookup%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1864942%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%20Thank%20you%20for%20taking%20pity%20on%20a%20learner.%26nbsp%3B%20I%20don't%20really%20understand%20what%20you%20mean%20by%20Option%20Explicit%20to%20force%20the%20declaration%20of%20variables.%26nbsp%3B%20It%20sounds%20like%20an%20important%20concept%20though%20as%20I%20move%20forward.%26nbsp%3B%20If%20you%20don't%20mind%20I'd%20like%20to%20understand%20a%20bit%20more.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20putting%20brackets%20around%20%5BH8%5D%2C%5BC%3AC%5D%2C%20etc.%26nbsp%3B%20I%20don't%20want%20to%20use%20%5BC2%3AC8%5D%20because%20the%20worksheets%20have%20differing%20number%20of%20rows%20and%20I'd%20like%20it%20to%20just%20search%20the%20entire%20column.%26nbsp%3B%20My%20result%20is%20that%20the%20cells%20G2%20and%20G3%20return%20nothing.%20Not%20even%20%22not%20found%22.%26nbsp%3B%20I'm%20perplexed.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hello, TY in advance for helping me.  I am learning to code VBA and looking for a VBA solution...I understand Xlookup in regular Excel.

 

I've included a data sample and the code.  The first half of the code works fine and retrieves the min or max value as asked for column H.  But my Xlookup isn't working for the three highlighted cells in column G.  I've googled my little heart out but can't figure out how to make it find the values in column H to retrieve the item name associated with it.  I've tried Cells(2,8) and I've tried MaxValue2.  

 

I've added the file and a screenshot of it.  Any thoughts?

 

CodeQ.PNG

 

4 Replies
Highlighted
Best Response confirmed by Danak995 (New Contributor)
Solution

@Danak995 

The line

Range("G2") = WorksheetFunction.XLookup(Cells(2, 8), "C2:C8", "A2:A8", "Not found")

 has strings as parameters rather than range references

Range("G2") = WorksheetFunction.XLookup([H8], [C2:C8], [A2:A8], "Not found")

where [A2:A8] is an alternative notation for Range("A2:A8").

 

Other comments are that it is good practice to use Option Explicit to force the declaration of variables; I prefer the use of Defined Names or Tables (List Objects) to the direct referencing to avoid problems when rows are added or deleted to the sheet.

Given the choice, I would use worksheet functions directly within the grid rather than from VBA, but I appreciate this is a learning exercise.  Some thing that may be of interest is that it is possible to write the worksheet formulas from VBA.

Range("G2").Formula = "= XLookup(H2, C2:C8, A2:A8)"

 

Highlighted

@Peter Bartholomew  Thank you for taking pity on a learner.  I don't really understand what you mean by Option Explicit to force the declaration of variables.  It sounds like an important concept though as I move forward.  If you don't mind I'd like to understand a bit more.

 

I tried putting brackets around [H8],[C:C], etc.  I don't want to use [C2:C8] because the worksheets have differing number of rows and I'd like it to just search the entire column.  My result is that the cells G2 and G3 return nothing. Not even "not found".  I'm perplexed.

Highlighted

@Peter Bartholomew  I'm so happy!  This finally worked:

Range("G2") = WorksheetFunction.xlookup(MaxValue, [C:C], [A:A], "Not found")

 

Thank you for your guidance!!!

Highlighted

@Danak995 

Option Explicit

Under Tools/Options/Editor there is an option button "Require Variable Declaration".  Once you set it, whenever you load a code module, it should start with Option Explicit.  The code will then only run if all variables are declared.  In this case

Dim MaxValue As Double, MaxValue2 As Double
Dim MinValue As Double

 

Certainly references such as A:A have a place in traditional Excel.  I think some care is needed because some array formulas will actually perform the 1048575 calculations, just as requested.  The Table has the advantage of adjusting dynamically as you add data.

Dim Rng As Range, Change As Range, Volume As Range, Item As Range

    Set Rng = Worksheets(1).ListObjects(1).DataBodyRange
    With Rng
        Set Change = .Columns(3)
        Set Volume = .Columns(4)
        Set Item = .Columns(1)
    End With
...
    [G2] = WorksheetFunction.XLookup(Cells(2, 8), Change, Item, "Not found")
    [G3] = WorksheetFunction.XLookup(Cells(3, 8), Change, Item, "Not found")
    [G4] = WorksheetFunction.XLookup(MaxValue2, Volume, Item, "Not found")