Solved

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

Posted on 2011-03-10
2
547 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

809 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