Solved

Using Autofill in Excel VBA

Posted on 2014-03-25
4
624 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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.

839 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