+ Reply to Thread
Results 1 to 9 of 9

Using VBA to create Vlookup Commands..

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Using VBA to create Vlookup Commands..

    I am essentially trying to use VBA to perform a Vlookup command, that increments the row, for example B2, B3, B4 and such.

    I have created the code below, to increment the row in the Vlookup, however it keeps debugging. Has anyone run into this issue? Thanks.

    
    lastsymbol = Cells(Rows.Count, "H").End(xlUp).Row
      For I = 2 To lastsymbol
    
          With Sheets("Data")
    .Cells(I, 8) = "=VLOOKUP(B" & I & ",'C:\Master Lookupxls]Project -> Year Lookup'!$A$1:$B$65536,2,0)"""
          End With
          
       Next I

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2507 Win 11
    Posts
    24,929

    Re: Using VBA to create Vlookup Commands..

    Try changing
    .Cells(I, 8) = "=VLOOKUP(B" & I & ",'C:\Master Lookupxls]Project -> Year Lookup'!$A$1:$B$65536,2,0)"""
    to
    .Cells(I, 8).Formula = "=VLOOKUP(B" & I & ",'C:\Master Lookupxls]Project -> Year Lookup'!$A$1:$B$65536,2,0)"""
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    01-11-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Using VBA to create Vlookup Commands..

    That worked, but a popup that says "Update Values: Master Lookup Sheet" comes up that asks for the location again of the excel file. It does that for each iteration of the for loop, i.e. for each row.

    Is there anyway the vba can run by by-passing this popup or entering in the location in the popup. Ty

  4. #4
    Registered User
    Join Date
    01-11-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Using VBA to create Vlookup Commands..

    That worked. Thanks

    This code lets you setup the Vlookup for the first row and then does an autofill for the rest of the rows below it. Thanks

    
    lastsymbol = Cells(Rows.Count, "H").End(xlUp).Row
    
    
          With Sheets("Data")
          
          
    .Cells(2, 8).Formula = "=VLOOKUP(B2" & ",'C:\[Master Lookup Sheet.xls]Project -> Year Lookup'!$A$1:$B$65536,2,0)"
        Range("H2").Select
        Selection.AutoFill Destination:=Range("H2:H2447")
        Range("H2:H2447").Select
        
          End With

  5. #5
    Registered User
    Join Date
    01-11-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Using VBA to create Vlookup Commands..

    It seems to work, however the pop-up still comes up at the end that says "Update Values: Master Lookup Sheet", looking for the location of the lookup file. Is there anyway to automatically hit cancel or type in the file path name in this popup?

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,720

    Re: Using VBA to create Vlookup Commands..

    I haven't looked any further but there are a couple of things to start with I think.

    .Cells(i, 8).Formula= "=VLOOKUP(B" & I & ",'[C:\Master Lookupxls]

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2507 Win 11
    Posts
    24,929

    Re: Using VBA to create Vlookup Commands..

    Here's an alternative
    .Range("H2:H" & lastsymbol).Formula = "=VLOOKUP(B2" & ",'C:\[Master Lookup Sheet.xls]Project -> Year Lookup'!$A$1:$B$65536,2,0)"
    You then don't need to either loop with a for statement or do an autofill. Try this and post back if you still have an issue.

  8. #8
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Using VBA to create Vlookup Commands..

    You could try this to get rid of that pop-up:
    Application.AskToUpdateLinks = False

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Why not add the full path for the lookup file to the formula?
    If posting code please use code tags, see here.

+ 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