Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

For loops instead of vlookup

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
hugoohta
Asked:
hugoohta
  • 6
  • 3
1 Solution
 
JeewsCommented:
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
 
hugoohtaAuthor Commented:
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
 
JeewsCommented:
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!

 
hugoohtaAuthor Commented:
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
 
JeewsCommented:
I have attached the excel also for your reference

new-help.xls
0
 
JeewsCommented:
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
 
hugoohtaAuthor Commented:
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
 
JeewsCommented:
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
 
JeewsCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now