+ Reply to Thread
Results 1 to 4 of 4

Question vlookup vba

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2017
    Location
    Mexico
    MS-Off Ver
    2013
    Posts
    9

    Question vlookup vba

    Hi friends¡ first post here i would like to know if its possible to do multiple vlook ups with this code (it doesnt work )

    Sub Matriz_costos()

    Dim i As Integer

    For i = 6 To 8

    Worksheets("Input").Cells(i, 8).Value = WorksheetFunction.VLookup(Cells(i, 7), Range("G1:H4"), 2, [0])

    Next i

    End Sub

    So in the cell H6 i want to look up the cell G6 in G1:H4 second column
    Then in the H7 i want to look up the cell G7 in G1:H4 second column
    Then in the H8 i want to look up the cell G8 in G1:H4 second column

    Furthermore i would want to search for a concatenated formula that contains the G6 and other information but in the same iterative way.

    Thanks for helping me.

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Question vlookup vba

    Try this code
    Sub Test()
        Dim i As Integer
        
        With Worksheets("Input")
            For i = 6 To 8
                .Cells(i, 8).Value = Application.WorksheetFunction.VLookup(.Cells(i, 7), .Range("G1:H4"), 2, 0)
            Next i
        End With
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: Question vlookup vba

    Better:

    Sub Test()
    Dim r As Range, i&    'Excel works with longs, your integer would be permanently converted to long; more - Cells property needs longs as parameters!
    With <write the CODENAME of the sheet "Input" here>    'Why are sooo loved ugly (slow to use for VBA) sheet names in this forum instead of simple quick-to-use codenames?
        Set r = .Range("G1:H4")    'Why to permanently repeat the construction of the range in the for-cycle body?
        For i = 6 To 8
            .Cells(i, 8) = WorksheetFunction.VLookup(.Cells(i, 7), r, 2, 0)    'worksheetfunction object can be called directly
        Next i
    End With
    End Sub
    More: DON'T use explicit addressing, use NAMED ranges!!! No
    Set r = .Range("G1:H4")
    that must be changed whenever the sheet is reorganized (by deleting or inserting rows or columns)! Give some workbook-scoped name to the range, say, 'lookTbl', and write

    Sub Test()
    Dim r As Range, i&
    Set r = [lookTbl]
    With <write the CODENAME of the sheet "Input" here>
        For i = 6 To 8
            .Cells(i, 8) = WorksheetFunction.VLookup(.Cells(i, 7), r, 2, 0)
        Next i
    End With
    End Sub
    Think yourself how to change i = 6 To 8 and Cells(i, 8), Cells(i, 7) to avoid making changes in code when the sheet is reorganized.
    Last edited by Jan Mach; 02-28-2017 at 07:05 PM.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Question vlookup vba

    I would agree that named ranges are a great method for setting ranges of cells a macro can use without needing to edit the VBA should the range need to change.

    I would not agree that codenames should be used over sheet names, that is a preference, one you obviously feel strongly about, but more issues will require code editing more often with codename usage over sheetname usage.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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. Vlookup Question-how do I manage the Vlookup?
    By athard in forum Excel General
    Replies: 5
    Last Post: 12-31-2009, 06:04 AM
  2. Vlookup question
    By freud1 in forum Excel General
    Replies: 10
    Last Post: 12-02-2008, 04:36 PM
  3. VLOOKUP question
    By PCMIKE in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-19-2008, 11:13 PM
  4. VLOOKUP question
    By duration in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-11-2006, 10:25 AM
  5. VLOOKUP question...it probably can't...
    By roger_home in forum Excel General
    Replies: 2
    Last Post: 02-15-2006, 07:57 PM
  6. [SOLVED] VLOOKUP question
    By bj in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 04:05 AM
  7. VLOOKUP question
    By Watercolor artist in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM
  8. [SOLVED] VLOOKUP question
    By Watercolor artist in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-13-2005, 04:05 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