?
Solved

For loops instead of vlookup

Posted on 2008-10-20
9
Medium Priority
?
635 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

770 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