Solved

For loops instead of vlookup

Posted on 2008-10-20
9
617 Views
Last Modified: 2011-10-19
Hi,

Basically I am trying to match using a For loop
the codes in Sheet2 to the names in Sheet1.

So each code must be given a name accordingly in sheet2.
Now I know this could be done easily using Vlookups and thats what
I am using for my current spreasheet which is reall huge and its
taking a long time to run the macro that does a vlookup for each row in sheet2.

So I need to find out how to do it using a for loop
Please include a button to execute the macro.

Thanks
new-help.xls
0
Comment
Question by:hugoohta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 6

Expert Comment

by:Jeews
ID: 22755711
I'm not too sure why the Vlookup took long time to execute as it is one of the fastest ways to active this

How ever try this code and let us know your feed back

Range("F2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],Sheet1!C[-5]:C[-4],2,FALSE)"
Range("F2").Select
Selection.AutoFill Destination:=Range("F2:F1000")

Here
F2 - First Cell of the Name Column
1000 - Number of cells you want the VLookup to be copied        
0
 

Author Comment

by:hugoohta
ID: 22755724
Thanks for your feedback but this is exactly what I am using right now to do it.
Keep in mind that my original spreadsheet is 62000 rows long.
I need a For loop rather than recording a macro that inserts the formula of a vlookup and
calculates for each cell filling.
Thanks for trying
0
 
LVL 6

Expert Comment

by:Jeews
ID: 22755771
Then check whether this is what you were looking for

Sub Update_Vlookup()
    lnRowCount = Sheets(2).UsedRange.Rows.Count
 
    For Each LoCell In ThisWorkbook.Worksheets(2).Columns(6).Rows
        If LoCell.Row > lnRowCount Then
            Exit Sub
        End If
        
        If LoCell.Row > 1 Then
            LoCell.Value = Application.VLookup(Worksheets(2).Cells(LoCell.Row, 3).Value, Worksheets(1).Range("A:B"), 2, False)
        End If
    Next
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hugoohta
ID: 22755798
Thanks

I will try to adapt that to my original spreadsheet
but my only concern is that this is still using a Vlookup...
Any other solutions are welcome as well
0
 
LVL 6

Expert Comment

by:Jeews
ID: 22755801
I have attached the excel also for your reference

new-help.xls
0
 
LVL 6

Accepted Solution

by:
Jeews earned 125 total points
ID: 22755844
As I mentioned in my initial post Vlookup is the fastest option for this, other than that we can use a search or another loop which is going to be slower than this.

If you really want to try any of these please let us know
0
 

Author Comment

by:hugoohta
ID: 22756010
Thanks a lot
but I have one more question for you

I have a code that looks like this:

Selection.AutoFill Destination:=Range("AA2:AA60000")

Which I do not want to go throught all 60 000 cells
so I want to find a way to add your variable

lnRowCount = Sheets("Sheet2").UsedRange.Rows.Count
and just Autofill from row 2 to row LnRowCount

how do I do that?
0
 
LVL 6

Expert Comment

by:Jeews
ID: 22756064
Try This

Range(Cells(2, 27), Cells(lnRowCount, 27)).Select

Here 27 is the column Index, as you are dealing with column AA the index is 27


0
 
LVL 6

Expert Comment

by:Jeews
ID: 22756069
Here is the exact code snippet





lnRowCount = Sheets("Sheet2").UsedRange.Rows.Count
 
Selection.AutoFill Destination:=Range(Cells(2, 27), Cells(lnRowCount, 27))

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question