Solved

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

Posted on 2011-03-10
2
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

617 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