+ Reply to Thread
Results 1 to 11 of 11

Defining a variable as Vlookup

  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Defining a variable as Vlookup

    I can't get the syntax right on this variable.

    Please Login or Register  to view this content.
    This is what the macro recorder picks up, but I might need to use the ranges like they appear in an excel cell. Overall, I'm confused about how to write functions in VBA as opposed to in the worksheet.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Defining a variable as Vlookup

    I've never seen WorksheetFunction in recorded code.

    What exactly did you record?

    Have you taken code like this and changed it?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Defining a variable as Vlookup

    Macro recorder records it like you wrote it. If I want to define a variable as this value (without directing it to a cell within the worksheet), how can I write it?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Defining a variable as Vlookup

    Like I said I've never seen the macro recorder produce code that includes WorksheetFunction.

    To use WorksheetFunction.VLookUp you need ranges for the lookup value and the lookup range.

    In what you've posted, RC2 means the cell in column 2 (B) ont the same row the formula is going in.

    You aren't putting the formula in a cell so it's hard to know what to replace RC2 with.

    The lookup range is easier it would be Worksheets("Accountability").Range("B:C").

  5. #5
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Defining a variable as Vlookup

    I'm just looking for an example of a function that would work so that I can substitute out my ranges etc.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Defining a variable as Vlookup

    I'm not sure what you mean, you can't do it without ranges.

    The code you posted is almose how it should be done - Accountablity should be Worksheets("Accountability"), as I posted, and it should be separated from Range by a dot.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,344

    Re: Defining a variable as Vlookup

    I doubt the macro recorder would have recorded this:

    Please Login or Register  to view this content.

    The code would look something like this:

    Please Login or Register  to view this content.

    Change Range("A2") to whatever you need as the lookup cell.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  8. #8
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: Defining a variable as Vlookup

    With either instances of code (using C2:C3 format or B:C format), VBA errors with "Unable to get the Vlookup property of the WorksheetFunction class".

    You are correct. VBA recorder did not record the entire line I wrote, but it wrote this part of it (below) and I plugged into the format that I've seen for other function variables:
    Please Login or Register  to view this content.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,344

    Re: Defining a variable as Vlookup

    If I put a VLOOKUP formula into cell B2 and then record refreshing that formula, I get:

    Please Login or Register  to view this content.

    So, in this instance, the formula was: =VLOOKUP(A2,Accountability!C2:C3,2,FALSE)

    But I could just as easily put the formula in directly

    Please Login or Register  to view this content.

    And I don't need the Select. I could use:

    Please Login or Register  to view this content.

    But, if you're trying to get the value returned by the VLOOKUP in a variable, maybe this is what you want:

    Please Login or Register  to view this content.

    Obviously, what you get in terms of relative references depend on which cell you put the formula into.


    Regards, TMS

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Defining a variable as Vlookup

    Can you post the exact code you are using?

    There might be nothing wrong with the code, the problem could be that the VLookup is actually failing.

    Have you tried testing the VLookup manually on a worksheet?

    By the way, if you use C2:C3 the formula will fail as it's only one column (2 cells actually) and the formula is looking to return the 2nd column of the lookup range.

    C2:C3, in R1C1 notation, translates as B:C in A1 notation.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,344

    Re: Defining a variable as Vlookup

    @Norie: bit of a crock of .... really. Sorry, didn't dawn on me that was only one column (two cells) though it did occur to me that the search was pretty limited.


    @jipstastic: I think you would save yourself and us a lot of time if you uploaded a sample workbook and explained what the formula is you are trying to evaluate and what you are trying to do with it.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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