+ Reply to Thread
Results 1 to 12 of 12

Runtime error when autofill of empty cells

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2010
    Location
    lichfield
    MS-Off Ver
    Excel 2007
    Posts
    9

    Runtime error when autofill of empty cells

    Hi,

    I have a macro that does a VLOOKUP and then autofills the results to a specified column. However, if there are zero results from the VLOOKUP it fails with a Runtime error. Do you know if there is any way around this?

    The code is below:

    Range("D1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(C[-2],Sheet2!C[-2]:C[-1],2,FALSE)"
    Selection.AutoFill Destination:=Range("D1:D" & lastrow)

    Thanks

  2. #2
    Registered User
    Join Date
    01-24-2012
    Location
    Swindon, England
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Runtime error when autofill of empty cells

    Hi

    Is the problem with not defining your last row. The code below worked OK.

    Regards

    Dominic

    Sub Macro1()
        lastrow = Range("B1").CurrentRegion.Rows.Count
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=VLOOKUP(C[-2],Sheet2!C[-2]:C[-1],2,FALSE)"
        Selection.AutoFill Destination:=Range("D1:D" & lastrow)
    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Runtime error when autofill of empty cells

    Another:

    Range("D1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(C[-2],Sheet2!C[-2]:C[-1],2,FALSE)"
    Selection.AutoFill Destination:=Range("D1:D" & Range("D" & rows.count).End(3)(1).row)

  4. #4
    Registered User
    Join Date
    04-16-2010
    Location
    lichfield
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Runtime error when autofill of empty cells

    Sorry, I should have said, I was defining "lastrow" as lastrow = Cells(Rows.Count, "A").End(xlUp).Row.
    I've tried both suggestions, but I still get the same Runtime error.

    Any other suggestions?

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error when autofill of empty cells

    Does this work?
        Range("D1:D" & lastrow).FormulaR1C1 = "=VLOOKUP(C[-2],Sheet2!C[-2]:C[-1],2,FALSE)"
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    04-16-2010
    Location
    lichfield
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Runtime error when autofill of empty cells

    No this doesn't work. It doesn't copy down, apart from the 1st row.

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Runtime error when autofill of empty cells

    Are you certain lastrow is not equal to 1?
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  8. #8
    Registered User
    Join Date
    04-16-2010
    Location
    lichfield
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Runtime error when autofill of empty cells

    Sorry, not sure what you mean?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error when autofill of empty cells

    Dominic

    That means there's a problem with lastrow.

    What happens if you change the code for it to this?
    lastrow = Range("B" & Rows.Count).End(xlUp).Row

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Runtime error when autofill of empty cells

    I think your lastrow variable = 1. This will cause error with your code and only 1 row of formulas with Norie's code.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error when autofill of empty cells

    ... and the error with the first code.

  12. #12
    Registered User
    Join Date
    01-24-2012
    Location
    Swindon, England
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Runtime error when autofill of empty cells

    Hi

    I still think the problem with your original code is the calculation of lastrow. I suggest you try putting a message box into your original code that displays the value of lastrow before you select D1 and get to the Autofill.

    Regards

    Dominic

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Error when averaging empty & non empty cells
    By simonlblea in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-09-2013, 09:04 AM
  2. Runtime Error "1004" Autofill Method of range class failed
    By DonKarlos in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-15-2009, 07:44 PM
  3. AutoFill Above in empty cells
    By excelgrrl in forum Excel General
    Replies: 3
    Last Post: 12-15-2009, 06:40 PM
  4. Macro to autofill a number in empty cells in a column
    By excelaspire0219 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-29-2009, 12:03 PM
  5. [SOLVED] Runtime Error Msg: "Cannot empty the Clipboard"
    By BW in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-30-2005, 12:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1