+ Reply to Thread
Results 1 to 8 of 8

VLookup with for loop

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    VLookup with for loop

    I have a probelm with vlookup function used with for loop.

    In the file attached you can see what i am trying to achive. I need values from e7:e11 that are corresponding to the letters in range c7:c11 to be allocated to the correspodning entries in another table. I need to use the function vlookup and for each cell.

    This i swhat i managed to workout s far but I am stuck now

    Sub newmacroo()
    
    
    
     On Error Resume Next
    
    
    Set batchrange = Range("i7:i11")
    
    Set myrange = Range("c7:e11")
    
    
    
    For Each cl In batchrange
    Cells(11, asd) = Application.WorksheetFunction.VLookup(cl, myrange1, 3, False)
    
    asd = asd + 1
    Next cl
    
    
    
    
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    09-28-2014
    Location
    harbin,china
    MS-Off Ver
    2007
    Posts
    162

    Re: VLookup with for loop

    try

    Sub newmacroo()
       On Error Resume Next
    
       Set batchrange = Range("i7:i11")
       Set myrange = Range("c7:e11")
    
       For Each cl In batchrange
          asd = asd + 1
          Cells(11, asd) = Application.WorksheetFunction.VLookup(cl, myrange, 3, False)
       Next cl
    End Sub

  3. #3
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: VLookup with for loop

    Quote Originally Posted by simcode View Post
    try

    Sub newmacroo()
       On Error Resume Next
    
       Set batchrange = Range("i7:i11")
       Set myrange = Range("c7:e11")
    
       For Each cl In batchrange
          asd = asd + 1
          Cells(11, asd) = Application.WorksheetFunction.VLookup(cl, myrange, 3, False)
       Next cl
    End Sub
    This is not pasting where it should.
    I need the values to be pasted using vlookup i.e. to corresponding entries in the right hand side table...

  4. #4
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: VLookup with for loop

    I was also thinking that i will need to use match function to find appropriate row number to paste a value but not sure

    Sub newmacroo()
       On Error Resume Next
    
       Set batchrange = Range("i7:i11")
       Set myrange = Range("c7:e11")
       
    For Each cell In batchrange
    
    asd = Application.WorksheetFunction.Match(celll, myrange, 0)
          
       asd1 = asd.Row
    
    Next cell
    
    
       For Each cl In batchrange
          Cells(asd1, 11) = Application.WorksheetFunction.VLookup(cl, batchrange, 3, False)
          
       
          
       
      
       
       Next cl
    End Sub

  5. #5
    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 with for loop

    ..Why do you need to use VBA at all? why not just enter the VLOOKUP in K7:K11 directly?
    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.

  6. #6
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: VLookup with for loop

    Quote Originally Posted by Richard Buttrey View Post
    ..Why do you need to use VBA at all? why not just enter the VLOOKUP in K7:K11 directly?
    My actual spreadsheet is much more complex and I have not pasted it here just created an example instead. The work I do needs to be done in a way I am asking .

    Thanks

  7. #7
    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 with for loop

    Quote Originally Posted by nosense View Post
    My actual spreadsheet is much more complex and I have not pasted it here just created an example instead. The work I do needs to be done in a way I am asking .

    Thanks
    Examples often don't reflect the production data which is why we more often than not prefer to see the real data, if only a subset.

    Since you are hardcoding the range i7:i11 it's not at all apparent why you NEED a macro. It would be nice to understand why you think that since that often helps us understand the overall process. Even if you do want a macro why not just write the VLOOKUP into the range

    i.e.

    Range("I7:I11") = "=Vlookup(lookup_value,lookup_range,lookup_column,False)"

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,875

    Re: VLookup with for loop

    Try (based on your original one):
    Sub newmacroo()
    dim batchrange as range, myrange as range, cl as range
    Set batchrange = Range("i7:i11")
    Set myrange = Range("c7:e11")
    For Each cl In batchrange
      On Error Resume Next
      cl.Offset(0, 2) = Application.WorksheetFunction.VLookup(cl, myrange, 3, False)
      On Error GoTo 0
    Next cl
    End Sub
    note that Richard's comments are still valid, I just corrected, what I seen as obviously wrong approach in your macro - using asd variable, mismatched rows and columns in Cells(11, asd), using myrange1 in vlookup...
    Last edited by Kaper; 12-08-2015 at 12:41 PM.
    Best Regards,

    Kaper

+ 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. VBA Loop within a Loop using Vlookup
    By inq80 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-04-2015, 05:28 PM
  2. [SOLVED] For loop vlookup
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2014, 10:03 AM
  3. Vlookup with Loop
    By noexcuseanymore in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-11-2012, 11:48 PM
  4. VlookUp with Loop?
    By ezek in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-05-2012, 06:24 AM
  5. Using Vlookup in a for loop
    By jbd99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2012, 07:57 AM
  6. Replies: 5
    Last Post: 07-29-2009, 07:53 AM
  7. Vlookup loop
    By mccrimmon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-25-2008, 08:53 AM

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