Autofill up

fabling

New Member
Joined
Jun 9, 2011
Messages
3
I'm fairly new to Excel, so bear please bear with me.

I'm trying to make a macro that autofills up. The range that it needs to fill changes, so I've made a string variable(insStr) that has the cell range needed.

I've recorded a macro while doing this manually and the code is exact the same aside from the variable. Also, my string has the same value as the range that comes up in the recorded macro (I've used a msgbox to double check). The recorded macro works fine when run, yet mine does not.

Error message is:

Run-time error '1004':

AutoFill method of Range class failed


What am I missing here? Are you not able to use a variable for the range?


Here is the code:
Range(insStr).Select
Selection.AutoFill Destination:=Range(insStr), Type:=xlFillDefault
Range(insStr).Select


Thank you,

Ben
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to MrExcel Board...

so if you manually try to do the same thing what happens? Does the variable make sense? Just something to consider
 
Upvote 0
It works when I do it manually and when the macro that I recorded while doing so is run.

Maybe the row values are transposed in the recorded macro(the first row value is greater than the second ex. A3:B2). I'll check that when I get back to my PC next week.

Thank you for the reply.
 
Upvote 0
Hi

Try this -

Code:
Dim Sourcerng As Range
Set Sourcerng = Range(insStr).Offset(Range(insStr).Rows.Count - 1).Resize(1)
Sourcerng.AutoFill Destination:=Range(insStr), Type:=xlFillDefault
Range(insStr).Select

The cell which you want to Autofill has to be part of the Range, in this case you want the last cell in the Range.

What you're doing is specifying the whole range to autofill itself which is causing the error.

The solution I have provided will use the last cell in the Range irrespective of the size of the Range.

You don't need to select the Range before autofilling, indeed you'll see it stated throughout the board that you rarely, if ever, have to Select a cell while using VBA.

If you specify Range addresses the wrong way round Excel will self-correct.

hth
 
Last edited:
Upvote 0
Or this which is a neater approach for setting the Sourcerng -

Code:
Dim Sourcerng As Range
Set Sourcerng = Application.WorksheetFunction.Index(Range(insStr), Range(insStr).Rows.Count)
Sourcerng.AutoFill Destination:=Range(insStr), Type:=xlFillDefault
Range(insStr).Select

hth
 
Upvote 0
Thank you for clarifying that Mike. Now that I look at it, I can see my error. I appreciate the help.


Ben
 
Upvote 0
Ben

Pleased to have helped solve your problem.

Thanks for the feedback.

Good luck with your project.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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