?
Solved

Using Autofill in Excel VBA

Posted on 2014-03-25
4
Medium Priority
?
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

719 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