+ Reply to Thread
Results 1 to 14 of 14

VBA Vlookup Problem

  1. #1
    Registered User
    Join Date
    08-13-2007
    Posts
    7

    VBA Vlookup Problem

    I use the following code for a vlookup in my VBA Macro:
    Please Login or Register  to view this content.
    When this is executed in Excel, the following results show (attached picture) incorrectly. The vlookup isn't calculating?!

    But then outside of the macro, when I click in the formula bar and press 'enter', it calculates it to a different result. Why isn't it calculating correctly with this code?

    I'd appreciate any help, guidance, or direction someone can offer.

    Thanks
    Attached Images Attached Images
    Last edited by ahwestrich; 08-13-2007 at 04:36 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    HI

    Is your calculation turned to automatic?


    rylo

  3. #3
    Registered User
    Join Date
    08-13-2007
    Posts
    7
    Yeah, the calculations are set to that.

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Can't see why, but the only thing I can think of is that it is getting hooked up on the R1C1 format

    Try

    Please Login or Register  to view this content.
    Also, do you really want it to go down that far? Will take up a lot of memory.....


    rylo

  5. #5
    Registered User
    Join Date
    08-13-2007
    Posts
    7
    i just tried that and it did not work either

    You're right about the memory...this is just a test to make sure I can get this macro to work as planned.

    Unfortunately this stumbling block has hindered my progress

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    What reference style do you have in your sheet? R1C1, or non R1C1?


    rylo

  7. #7
    Registered User
    Join Date
    08-13-2007
    Posts
    7
    I used the R1C1

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try changing it to the non R1C1 and run the code. If it calculates properly that way, then add the conversion to the start of the code, then turn it back again at the end. Record a macro to get the code to do those steps.

    If that doesn't work, then let us know...

    rylo

  9. #9
    Registered User
    Join Date
    08-13-2007
    Posts
    7
    I've tried using both R1C1 and non and both didn't work. Recording the macro hasn't worked either because it gives me that same code. Is there code for the cursor placed in the formula bar then to press enter. That is what I have to do in order for it to calculate correctly.

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    If the workbook isn't too large, then try dragging each of the sheets to a new workbook, save it and see if that will work with the R1C1 set to the A1 format.

    If that doesn't work, then the only thing I can think of is to go down the column and make each of the formulas equal to the formula. This should have to same effect as editing, but (a) you shouldn't need to do it and (b) it will take some time.

    Any chance of putting up the workbook, or an example file that is doing the same thing???

    rylo

  11. #11
    Registered User
    Join Date
    08-13-2007
    Posts
    7
    Hey Rylo,
    Thanks for putting in the time to try and solve this problem for me. I'm working with some sensitive data that I'm probably not supposed to the internet so I changed it around a bit. Here's the perfect example of what's happening for me in this macro. This vlookup was generating from the code above. Column F evaluates to Found when if you look at the second worksheet, "Z", is clearly not found. But if you take each cell individually (i.e. G2), put the cursor in the formula bar, and press enter, it calculates it! Why wouldn't it calculate it the first time?
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    I modified the code so that it will only go down to the last entry in column G.

    This shouldn't make any difference to anything.

    Make sure you are using the A1 reference style, open the file, and run the macro. Works OK for me.


    rylo
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    08-13-2007
    Posts
    7
    Alright it worked! But what the heck!? For lessons learned, besides the efficiency factor, what is the difference between:
    Please Login or Register  to view this content.
    and the working version:
    Please Login or Register  to view this content.
    I appreciate your help Rylo!

  14. #14
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    I think I may have stumbled across something. When I changed to the R1C1 format, and put the formula in manually, the section lookup!A:A became Lookup!c[-5] (d'oh - silly me!!!)

    So when I changed the code to
    Please Login or Register  to view this content.
    it worked. Best I can think of is that it was treating A:A as a range name, and erroring out as there was no range name!

    See if you can get it working in R1C1 mode with the above code.


    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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