VBA - Error in code

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello, can anyone spot the obvious error in this code that is the reason for it not running please?

Code:
Private Sub Worksheet_Calculate()

Sheets("Data").Range("C" & Sheets("Data").Range("AD2")).AutoFill Destination:=Range("C" & Sheets("Data").Range("AD2") & ":C" & Sheets("Data").Sheets("Data").Range("D" & Rows.Count).End(xlUp).Row)

End Sub
I am receiving runtime error 438. In cell AD2 the row is dynamic, but it is currently 2.

Thanks.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
Sheets("Data").Range("C" & Sheets("Data").Range("AD2")).AutoFill Destination:=Sheets("Data").Range("C" & Sheets("Data").Range("AD2") & ":C" & Sheets("Data").Sheets("Data").Range("D" & Rows.Count).End(xlUp).Row)
Altered slightly and still doesn't work :S
 
Upvote 0
Try
Code:
Private Sub Worksheet_Calculate()

Sheets("Data").Range("C" & Sheets("Data").Range("AD2")).AutoFill Destination:=Range("C" & Sheets("Data").Range("AD2") & ":C" & Sheets("Data").Range("D" & Rows.Count).End(xlUp).Row)

End Sub
 
Upvote 0
try this
Code:
Private Sub Worksheet_Calculate()
    With Sheets("Data")
        .Range("C" & .Range("AD2")).AutoFill .Range("C" & .Range("AD2"), .Range("C" & .Range("D" & Rows.Count).End(xlUp).Row))
    End With
End Sub
 
Upvote 0
try this
Code:
Private Sub Worksheet_Calculate()
    With Sheets("Data")
        .Range("C" & .Range("AD2")).AutoFill .Range("C" & .Range("AD2"), .Range("C" & .Range("D" & Rows.Count).End(xlUp).Row))
    End With
End Sub

Thanks.

Trying to make this autofill dynamic by adding 2 cell variables - any ideas what I am doing wrong?

With Sheets("Data")
.Range("A" & .Range("AD8") & ":" & "A" & .Range("AD10")).AutoFill.End(xlUp).Row
End With
Thanks.
 
Upvote 0
Before anything else, do either of the 2 suggestions, do what you originally asked for?
 
Upvote 0
Ok, thanks for that.
For the modification, the code will be the same, except you need to change the column reference & also
Code:
Range("D" & Rows.Count).End(xlUp).Row)
needs to be replaced with
Code:
Range("AD10")
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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