Solved

how to drag vlookup in vba for all the cells down with dynamic range

Posted on 2011-03-10
2
538 Views
Last Modified: 2012-05-11
how to drag vlookup in vba for all the cells down
I have written

 
     Cells(4, Newcolumn).Formula = Application.WorksheetFunction.VLookup(Range("A4"), Sheets("Basic_Data").Range("C2:J7129"), 8, False)

Selection.AutoFill Destination:=Cells(newrange & ":" & r & "7129")
 Range(newrange & ":" & r & "7129").Select

but this is copying only values not formula. I need to use dynamic range
0
Comment
Question by:Ruchi_Sas
2 Comments
 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 125 total points
ID: 35095767
use this instead:

Range(newrange & ":" & r & "7129").FormulaR1C1 = "=VLOOKUP(RC1,Basic_Data!R2C3:R7129C10,8,0)"

now, if you want the 7129 to be dynamic, you could count the number of items on column C...
dim lngItems as Long
lngItems = application.worksheetfunctions.counta(Sheets("Basic_Data").Range("C1:C65000"))
Range(newrange & ":" & r & lngItems).FormulaR1C1 = "=VLOOKUP(RC1,Basic_Data!R2C3:R" & lngItems & "C10,8,0)"

0
 
LVL 6

Expert Comment

by:royhsiao
ID: 35097075
here is another 2 ways to find the last rows
Sub Macro1()
Dim i As Integer
    
    i = ActiveSheet.UsedRange.Rows.Count
 
    MsgBox i
    
End Sub


Sub macro2()
Dim i As Integer
    i = Range("C65536").End(xlUp).Row
    MsgBox i

End Sub

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

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!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now