Solved

For loops instead of vlookup

Posted on 2008-10-20
9
585 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now