Solved

For loops instead of vlookup

Posted on 2008-10-20
9
609 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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

807 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