+ Reply to Thread
Results 1 to 5 of 5

Vlookup in VBA using variable as datasheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Vlookup in VBA using variable as datasheet

    Hi,

    I'm trying to create the below lookup in VBA using a variable as the datasheet to lookup to. I get an error of "Object does not support this Property or Method" any ideas why


    Sub lookuptoanothersheet()
    Dim wbkmain As Workbook
    Dim wbkdatasheet As Workbook
    
    Set wbkmain = ThisWorkbook
    
    Set wbkdatasheet = Workbooks.Open("C:\Users\james\Documents\VBA\example.xlsx")
    
    wbkmain.Worksheets(1).Range("b1").Formula = "vlookup(A1,[" & wbkdatasheet & "]Table1!A1:C200,2,0)"
    
    
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Vlookup in VBA using variable as datasheet

    Dim wbkdatasheet As Workbook
    why do you call it a sheet when its a workbook? that is confusing.

    at a guess you need to make it the string(name) in the formula not a sheet object

    also you need and = sign

    wbkmain.Worksheets(1).Range("b1").Formula = "=vlookup(A1,[" & wbkdatasheet.name & "]Table1!A1:C200,2,0)"
    Last edited by scottiex; 10-07-2017 at 06:57 PM.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Vlookup in VBA using variable as datasheet

    In the line that sets the formula use

    Formula: copy to clipboard
    wbkdatasheet.Name

    and don't forget the = sign
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Vlookup in VBA using variable as datasheet

    Perfect Thanks Richard

  5. #5
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Vlookup in VBA using variable as datasheet

    good point re sheet

    adding the equals sign doesn't make a difference. I still get the same error

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] General VLOOKUP setup on my datasheet enquiry
    By gtz101 in forum Excel General
    Replies: 13
    Last Post: 07-10-2014, 05:30 AM
  2. Open variable name workbook & vlookup to find that variable named wookbook
    By vh_llc@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2014, 07:49 PM
  3. VBA for display Various Datasheet data in Masterdatasheet when we choose datasheet name
    By santosh226001 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-08-2014, 10:21 AM
  4. vlookup variable filepath one file variable can this be done via a function?
    By pauldbaker in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-15-2013, 10:10 AM
  5. vlookup with variable range and variable array size
    By chaslie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2013, 02:37 PM
  6. [SOLVED] Can I identify variables using VLOOKUP, and then pass variable to another VLOOKUP?
    By Jeff C in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-05-2012, 05:54 PM
  7. Variable File Reference and Variable Table Array in VBA VLOOKUP
    By Gingeiko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2011, 11:08 PM

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