Error when selecting range of variable size.

Kirby1996

New Member
Joined
Sep 26, 2019
Messages
2
Hi-

I haven't had much need to write VBA code in a few years, so I'm pretty sure this is straightforward.

I'm trying to copy paste a range of variable size and am getting a '1004' error when selecting to the right.

Basically, I have input data, and then output data which applies formulas to the input data. The size of the input data can vary, so I wish to paste some formulas to a range of the same length as the input data. The macro is initiated by an ActiveX button on another worksheet.

My code is as follows:

Code:
Private Sub CommandButton1_Click()


'Count rows in H&A file


Dim last_row As Long


last_row = Worksheets("H&A Input").Cells(Rows.Count, 1).End(xlUp).Row


MsgBox (last_row)

'Paste formulas to range the same length as H&A Input

Worksheets("H&A Output").Activate
Worksheets("H&A Output").Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFill Destination:=Range("A5:A" & CStr(last_row)), Type:=xlFillDefault


End Sub

As mentioned, the error is occurring on the Range(Selection... line.

Any help?

Much appreciated!

Regards...
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the forum.

It looks like your code is probably in a worksheet code module, so Range without a worksheet qualifier, refers to that sheet, not the active sheet. Also, the autofill destination has to start on the same row as you are filling from, and must include all the columns of the source range. So, for example:

Code:
With Worksheets("H&A Output")
With .Range("A4", .Range("A4").End(xlToRight))
    .AutoFill Destination:=.Resize(last_row - 3), Type:=xlFillDefault
end with
end with
 
Last edited:
Upvote 0
Welcome to the forum.

It looks like your code is probably in a worksheet code module, so Range without a worksheet qualifier, refers to that sheet, not the active sheet. Also, the autofill destination has to start on the same row as you are filling from, and must include all the columns of the source range. So, for example:

Code:
With Worksheets("H&A Output")
With .Range("A4", .Range("A4").End(xlToRight))
    .AutoFill Destination:=.Resize(last_row - 3), Type:=xlFillDefault
end with
end with

Ahh. Thanks. Not just the fix, but the background on the scope of the code within the worksheet. That's the key I needed.

Appreciate it!
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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