VLookUp not returning expected results

  • Hello,


    I have this code. It does exactly what is required even if the range("a2:d15") is not sorted.



    Then I have this code that doesn't work properly. It works to a certain point, but where the VLookUp should be returning a result it's doing nothing. The Application.WorksheetFunction.VLookUp looks to my eye to be the same in both lots of code.


    Any suggestions as to why the second lot of code doesn't play?


    Regards

  • Re: VLookUp not returning expected results


    Quote

    It works to a certain point, but where the VLookUp should be returning a result it's doing nothing.


    But you don't tell us where that is, so?

    Bruce :cool:

  • Re: VLookUp not returning expected results


    Quote from reddale;772719

    but where the VLookUp should be returning a result it's doing nothing


    Quote from skywriter;772743

    But you don't tell us where that is, so?


    In the second group of code, VLookUp isn't returning a result, it isn't doing anything

  • Re: VLookUp not returning expected results


    I could probably figure this out in just a few minutes with a workbook, but my hunch is your problem lies with what you are trying to look up.


    First why does what is in this cell have to be declared as a string?

    Code
    Dim acqstk As String
    acqstk = Sheet17.Range("b40")


    Instead of putting that variable in the lookup you could just simply put Sheet17.Range("b40").Value instead.


    The item you are looking up must be in the first column in the lookup data.


    I suggest you make a formula in a blank cell.
    =B40=?
    You know what's in B40 so look in the first column of the lookup data and find the cell the match is in and put that cell's address into where the question mark is.


    If the result of the formula is FALSE then there's an issue and you are not getting a match since Excel doesn't see them as equal.


    One example of where this happens is when numbers are imported and they have decimals that don't show up in the cells but exist in the underlying number.


    One cell actually has 1.0005 and the lookup cell has 1.00. The user sees 1.00 in both cells because of the way they are formatted, but Excel doesn't see these as equal and they don't match.


    I don't know what your data is, but my hunch is the issue is the values are not equal.


    You might also want to comment out your on error statement to see if you get any errors and what they are and what line they are on.


    If none of that helps I suggest uploading a workbook.

    Bruce :cool:

  • Re: VLookUp not returning expected results


    Thank you for the detailed reply skywriter


    I will work through your suggestions and report back.

  • Re: VLookUp not returning expected results


    Bear with me, there are a few answers


    Quote from skywriter;772749

    I could probably figure this out in just a few minutes with a workbook, but my hunch is your problem lies with what you are trying to look up.


    I started to make a sample workbook to post, but it ends up being too large and the names, addresses etc of our clients are too numerous to replace.


    In the instance of the first piece of code I posted, it is text formatted as General


    In the instance of the second piece of code I posted, it is a number formatted as General


    Quote

    First why does what is in this cell have to be declared as a string?

    Code
    Dim acqstk As String
    acqstk = Sheet17.Range("b40")


    Instead of putting that variable in the lookup you could just simply put Sheet17.Range("b40").Value instead.


    Changing this worked...but


    This is where I get confused because of lack experience I guess


    When the first sample of code I posted runs (via a Worksheet_Change event) it works. When I say it works, the user enters a name in a cell and the other cells poplulate with data from another sheet. It works whether the range is sorted or not.


    When the second code sample runs (fired by clicking a command button) and the user enters a number in a cell, nothing happens. Leave that number in the cell and run the code again and it works.


    Quote

    You might also want to comment out your on error to see if you get any errors and what they are and what line they are on.


    I had gone down that roadalready, the error appears in the first line of the VLookUp code until a number is entered in B40 and the code is ran again, then there is no error

  • Re: VLookUp not returning expected results


    While I was trying to make my above answer make sense (still don't know if it does)


    I came up with a solution that invovles Data Validation.


    Once I did that, my code does what I wanted to do. I also found a couple of errors in related code, so a couple more things work better now as well.


    With my issues sorted, the new code (2nd lot in the original post) runs in half the time that the old code I was using that placed VLookUp formulas into the cells did. I don't know if half a second is significant when talking about VBA, but thats' the result I got


    skywriter, your suggestion about the string variable got the ball rolling.


    Thank you

  • Re: VLookUp not returning expected results


    Just as an additional point, it's quite inefficient to lookup the same lookup value repeatedly, and also to create a range variable just to perform one operation. You can reduce your code quite a lot - for example:

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: VLookUp not returning expected results


    Thank you Rory. I don't think I'll ever get to the level of you guys. Thats why I come here. Thank you for the code. It sure is shorter than mine. I'll have to learn a bit more a bout your code whe I get a chance.


    Regards

  • Re: VLookUp not returning expected results


    Quote from reddale;772856

    I don't think I'll ever get to the level of you guys.


    There's no reason why not - it just takes time. We all started from scratch at some point. :)

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!