Macro to fill down all cells from a vlookup on a different sheet

JadonR

Board Regular
Joined
Apr 15, 2009
Messages
154
Office Version
  1. 365
I would like to create a macro that can fill down cells A2 through C in Sheet3, based on how many rows there are in Sheet 1. For example, I have 20 rows on Sheet 1...

Sheet1.JPG


And the macro should copy down all cells in A2 through C and stop with the total of rows from Sheet1.
Sheet3.JPG


I created the basic macro below to illustrate what I want.

VBA Code:
Sheets("Sheet3").Activate
Range("A2:C2").Select
Selection[B].AutoFill Destination:=Range("A2:C20")[/B], Type:=xlFillDefault

I would like the AutoFill Destination range to match the number of rows from Sheet1 which will dynamically change when fresh data is copied into that sheet.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Maybe try....

VBA Code:
    With Sheets("Sheet3").Range("A2:C2")
        .AutoFill Destination:=.Resize(Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault
    End With
 
Upvote 0
Wow. That worked really well. What if I wanted to change it to the active sheet rather than "Sheet3"?
 
Upvote 0
Just remove the
VBA Code:
Sheets("Sheet3").
(not that I'm a lover of using ActiveSheet in code)
 
Upvote 0
I'm currently getting a debug error and the macro is failing to fill down. The debug error is highlighting this code:

VBA Code:
.AutoFill Destination:=.Resize(Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row - 1), Type:=xlFillDefault

I'm not clear why this happens. Any ideas where I should start?
 
Upvote 0
It displays "Run-time error '1004':
Application-defined or object-defined error
 
Upvote 0
What does the message box say when you run the code below
VBA Code:
Sub Testit()
  MsgBox Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
End Sub
 
Upvote 0
Then you can't resize by 0 (the row number from sheet1 minus 1 as your autofill data starts on row 2), and you also can't Autofill to the same row. Your Sheet1 only has a header or is blank in column A, your cells you are filling start on Row 2
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,926
Members
449,479
Latest member
nana abanyin

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