Solved

Using Autofill in Excel VBA

Posted on 2014-03-25
4
590 Views
Last Modified: 2014-03-31
I keep getting "Run-time error 1004", AutoFill method of range class failed"
I just want to automatically autofill Columns A to N to the using the latest non-empty row.

How should I revise the following?

Sub sadikyarim()
Dim lastrow As Long
     
    lastrow = Worksheets("sheet2").Range("N8").End(xlDown).Row
    With Worksheets("Sheet2").Range("A8:N8").End(xlDown)
        .AutoFill Destination:=Range("A8:N" & lastrow&)
    End With

  End Sub
0
Comment
Question by:awesomejohn19
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 500 total points
ID: 39954337
How about?

Sub sadikyarim()
Dim lastrow As Long
     
    lastrow = Worksheets("sheet2").Range("N8").End(xlDown).Row
    Worksheets("Sheet2").Range("A8:N" & llastrow).formular1c1=Worksheets("Sheet2").Range("A8").formular1c1

  End Sub 

Open in new window



In your code, the issue is the last &

        .AutoFill Destination:=Range("A8:N" & lastrow&)

should be

        .AutoFill Destination:=Worksheets("Sheet2").Range("A8:N" & lastrow)

Since the destination range is not defined as a range of Worksheets("Sheet2"), if your activesheet is anything else than Sheet2, it will also fail.

Thomas
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39955739
You should also be autofilling from row 8, not from Range("A8:N8").End(xlDown):

With Worksheets("Sheet2").Range("A8:N8")
        .AutoFill Destination:=Range("A8:N" & lastrow&)
    End With

Open in new window

0
 

Accepted Solution

by:
awesomejohn19 earned 0 total points
ID: 39956816
I modified the code nutsch posted and it works.

Sub sadikyarim()
Dim lastrow As Long
     
    lastrow = Worksheets("sheet2").Range("N8").End(xlDown).Row
    lastrow = lastrow + 1
    Worksheets("Sheet2").Range("A" & lastrow & ":N" & lastrow).FormulaR1C1 = Worksheets("Sheet2").Range("A" & lastrow - 1 & ":N" & lastrow - 1).FormulaR1C1
Range("A" & lastrow) = Range("A" & lastrow) + 1
  End Sub
0
 

Author Closing Comment

by:awesomejohn19
ID: 39966131
I did not address the question correctly. I should have said I would like to autofill using the last row instead of the first row of the series.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now