Strange behaviour VBA Autofill

mellymelle

New Member
Joined
Nov 25, 2016
Messages
45
Have a block of code that utilized .AutoFill several times. One of them returned a run-time error 1004 autofill method of range class failed. Couldn't for the life of me figure out why the other autofill codes weren't generating errors when they were in the exact same format (and were executing on the workbook - formulas were filled down as coded), so gave up and used copy paste instead on the code generating the error. The next run, another autofill code line that was executing fine before turned up a 1004 error. I didn't change the line in question at all, why would it work before and not now?

Here's a sample of the code, the original autofill line is included (but commented out) for illustration:

Code:
    Sheet3.Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Sheet3.Range("A1").Formula = "Account #"
    Sheet3.Range("A2").Formula = "=LEFT(TRIM(RC[8]),3) & MID(TRIM(RC[8]),5,4) & RIGHT(TRIM(RC[8]),1)"
    If ALastRow > 2 Then
        Sheet3.Range("A2").Copy Sheet3.Range("A3:A" & ALastRow)
'        Sheet3.Range("A2").AutoFill Destination:=Range("A2:A" & ALastRow), Type:=xlFillDefault
    End If
My workaround is working fine, but I'm perplexed. Please share your thoughts, thanks in advance.
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try: Sheet3.Range("A2").AutoFill Destination:=Sheet3.Range("A2:A" & ALastRow), Type:=xlFillDefault

(Without this qualification, your code will give a run-time error 1004 if Sheet3 is not the Active Sheet)
 
Upvote 0
Or in one line:

Sheet3.Range("A2:A" & ALastRow).Formula = "=LEFT(TRIM(I2),3) & MID(TRIM(I2),5,4) & RIGHT(TRIM(I2),1)"
 
Upvote 0
Another option
Code:
Sheet3.Range("A2:A" & ALastRow).FormulaR1C1 = "=LEFT(TRIM(RC[8]),3) & MID(TRIM(RC[8]),5,4) & RIGHT(TRIM(RC[8]),1)"
 
Upvote 0
Try: Sheet3.Range("A2").AutoFill Destination:=Sheet3.Range("A2:A" & ALastRow), Type:=xlFillDefault

(Without this qualification, your code will give a run-time error 1004 if Sheet3 is not the Active Sheet)

Thank you Stephen, that completely explains it. And thanks for your suggestions - it must be completely obvious that I'm super green at this!
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,883
Members
449,477
Latest member
panjongshing

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