Loop help

wisemank

Board Regular
Joined
Jun 21, 2010
Messages
129
Last question for today...I hope.


I want to find the last row with data and call that value from the loop in my vlookup formula (bold red), as opposed to current hard coded values. These amount of rows will be different each time I run.


Here is my code:

Sheets("VSC to FGA").Select
Range("D2").Select
Range("D2:D457" & LR).Formula = "=IF(ISERROR(VLOOKUP($C2,'Matricola Map'!B:G,4,FALSE)),""Not Found"",(VLOOKUP($C2,'Matricola Map'!B:G,4,FALSE)))"
Range("E2:E457" & LR).Formula = "=IF(ISERROR(VLOOKUP($D2,'Matricola Map'!E:F,2,FALSE)),""Not Found"",(VLOOKUP($D2,'Matricola Map'!E:F,2,FALSE)))"
Range("D2:E457").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Application.CutCopyMode = False

Range("A1").Select
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Code:
Range(Range("D2"), Range("D2").End(xlDown)).Formula = ...
Range(Range("E2"), Range("E2").End(xlDown)).Formula = ...
Range(Range("D2"), Range("F2").End(xlDown)).Select

this assumes there are no breaks (blank cells) between row 2 and the last cell with values in each column.

If there are blanks then to find the last cell use the following adaptation:
Code:
Range(Range("D2"), Range("D" & Rows.Count).End(xlUp)).Formula = ...
Range(Range("E2"), Range("E" & Rows.Count).End(xlUp)).Formula = ...
Range(Range("D2"), Range("E" & Rows.Count).End(xlUp)).Select
 
Last edited:
Upvote 0
Create a variable for the last row
LR = Range("A" & Rows.Count).End(xlUp).Row

then you can use that variable in your work
Code:
 Range("D2:D" & LR).Formula = "=IF(ISERROR(VLOOKUP($C2,'Matricola Map'!B:G,4,FALSE)),""Not Found"",(VLOOKUP($C2,'Matricola Map'!B:G,4,FALSE)))"
 
Upvote 0
Option 2 with Blanks worked and very fast.
Option 1 worked, but took a very long time.

You guys are the best...Thanks:biggrin:
 
Upvote 0
Option 2 with Blanks worked and very fast.
Option 1 worked, but took a very long time.

You guys are the best...Thanks:biggrin:


Strange that either took a long time since they do basically the same thing. I tested all on my computer and even when the range was 1M rows it finished nearly instantly for me. At any rate, glad it is up and running now.
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,906
Members
449,132
Latest member
Rosie14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top