Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-10
2
Medium Priority
?
607 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 500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

971 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