VBA for vlookup with dynamic range

blnswamy

New Member
Joined
Oct 18, 2013
Messages
8
Hi
I am trying to write a code with vlookup. The purpose of the code is to reference a data set in another sheet using vlookup and show the corresponding values. The data sets may may be varying in sizes. So i want the vlookup to go to the last cell possible with the data.

I have tried these two ways of calling vlookup, but nothing seems to work.
1.
Code:
  With Worksheets(2).Range("B1")                            
        .Formula = "=vlookup(A1,Sheet1!'$A1:$F50,1,FALSE)"       
        .Value = .Value
  End With
  Range("B1").Select
  Selection.Autofill Destination:=Range("B1:B50")

2.
Code:
Range("B1").Select
ActiveCell.FormulaR1C1 = "=Vlookup(A1,Sheet1!'$A1:$F50,1,FALSE)"
Range("B1").Select
Selection.Autofill Destination:=Range("B1:B50")

In sheet 2 range can change depending on the data set. that is what i want to be flexible. The autofill range also needs to be flexible, which i am unable to do.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
i have also used this code to have dynamic range of cells
Code:
 Range("B1").Select
        With Selection
            .Formula = "=vlookup(a1,Postupdate!'$A1:$F50,1,FALSE)"
            With .Resize(Range("B1" & Rows.Count).End(xlUp).Row - 1)
                    .FillDown
                    .Copy
                    .PasteSpecial x1PasteValues
                End With
            End With

but it fails at .resize line
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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