+ Reply to Thread
Results 1 to 9 of 9

Autofill Until is Empty

Hybrid View

  1. #1
    Registered User
    Join Date
    11-03-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Autofill Until is Empty

    Hi Holger,
    You're right, this did not work perfectly unfortunantely
    I have run across more trouble. Without having to build several macros and have them called for each step I have the following macro. Basically Columns E, F, G H and I will all be autofilled (with different inputs) the autofill should autofill until column A is empty for all. The below code works fine if I have data that goes past cell A2 as soon as I have data that finishes at A2 the macro errors with '1004 instead of exiting the sub.
    Sub AAA()
    
    Dim lastrow As Long
    lastrow = Worksheets("07DS").Range("A" & Rows.Count).End(xlUp).Row
    
    With Worksheets("07DS").Range("E2")
    If .Value <> "" Then
    .AutoFill Destination:=Range("E2:E" & lastrow&), Type:=xlFillCopy
    
    With Worksheets("07DS").Range("F2")
    .AutoFill Destination:=Range("F2:F" & lastrow&), Type:=xlFillCopy
    
    With Worksheets("07DS").Range("G2")
    .AutoFill Destination:=Range("G2:G" & lastrow&), Type:=xlFillCopy
    
    With Worksheets("07DS").Range("H2")
    .AutoFill Destination:=Range("H2:H" & lastrow&), Type:=xlFillCopy
    
    With Worksheets("07DS").Range("I2")
    .AutoFill Destination:=Range("I2:I" & lastrow&), Type:=xlFillCopy
        
    
    Exit Sub
    End With
    End With
    End With
    End With
    End If
    End With
    End Sub
    Any suggestions?

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Autofill Until is Empty

    Hi, Dnass,

    maybe you should have another look at my posts in this thread for finding out what was changed in order to make the code work without error. You used the very same new Variable as in the opening post - why? The code by Sixthsense works with a With-Statement but neglects the Autofill range to be related to that worksheet.

    Regarding your question: put in another if to check if the value of the Variable holding the last filled row in Column A is greater 2 (I choose <=2 here).

    Sub AAA_1()
    
    Dim lngLR As Long
    
    With Worksheets("07DS")
        lngLR = .Range("A" & Rows.Count).End(xlUp).Row
        If lngLR <= 2 Then Exit Sub
        If .Range("E2").Value <> "" Then .Range("E2").AutoFill Destination:=.Range("E2:E" & lngLR), Type:=xlFillCopy
        If .Range("F2").Value <> "" Then .Range("F2").AutoFill Destination:=.Range("F2:F" & lngLR), Type:=xlFillCopy
        If .Range("G2").Value <> "" Then .Range("G2").AutoFill Destination:=.Range("G2:G" & lngLR), Type:=xlFillCopy
        If .Range("H2").Value <> "" Then .Range("H2").AutoFill Destination:=.Range("H2:H" & lngLR), Type:=xlFillCopy
        If .Range("I2").Value <> "" Then .Range("I2").AutoFill Destination:=.Range("I2:I" & lngLR), Type:=xlFillCopy
    End With
    End Sub
    This code will check each single column before filling.

    You may combine this to read
    Sub AAA_2()
    
    Dim lngLR As Long
    
    With Worksheets("07DS")
        lngLR = .Range("A" & Rows.Count).End(xlUp).Row
        If lngLR <= 2 Then Exit Sub
        If .Range("E2").Value <> "" Then .Range("E2:i2").AutoFill Destination:=.Range("E2:I" & lngLR), Type:=xlFillCopy
    End With
    End Sub
    HTH,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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] autofill until adjacent cell empty
    By Hassan1977 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-03-2013, 01:54 PM
  2. [SOLVED] How to autofill any formula till last empty cell
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-16-2013, 08:56 AM
  3. Macro for Autofill removes header info when there is no data to autofill
    By esturan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-06-2012, 01:42 PM
  4. AutoFill Above in empty cells
    By excelgrrl in forum Excel General
    Replies: 3
    Last Post: 12-15-2009, 06:40 PM
  5. 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

Tags for this Thread

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