+ Reply to Thread
Results 1 to 6 of 6

Auto-fill to last column of data and then to last row of data

Hybrid View

  1. #1
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Auto-fill to last column of data and then to last row of data

    Hello to everyone,

    I would like to look for the last used cell in column 1, then grab L4 and auto-fill it up to the last used cell in column 1. Then auto-fill the range down to the last used row.

    In the attachment, AQ1 is the last used cell, so I would like to auto-fill L4 to AQ4. Then grab L4:AQ4 and auto-fill it down to row 41.

    Hopefully someone can help with the above as I've been struggling for a day editing other macros I've found online with no success.

    Thank you
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Auto-fill to last column of data and then to last row of data

    Try on a copy of your data
    Sub Sans()
        Dim Lrow As Long, Lcol As Long
        Lrow = Cells(Rows.Count, 12).End(xlUp).Row - 2
        Lcol = Cells(1, Columns.Count).End(xlToLeft).Column
        Range("L4").AutoFill Destination:=Range(Cells(4, 12), Cells(4, Lcol)), Type:=xlFillDefault
        Range(Cells(4, 12), Cells(4, Lcol)).AutoFill Destination:=Range(Cells(4, 12), Cells(Lrow, Lcol)), Type:=xlFillDefault
    End Sub
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Auto-fill to last column of data and then to last row of data

    Hi WasWodge,

    The code works perfectly, thank you! I was wondering if you could help me with a slight issue I am having. In case I have other data below the MIN and MAX, the auto-fill moves after the MIN and MAX.

    Is it possible to also give a quick explanation of each line in the code as I'd really like to understand how it works?

    Thank you!
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Auto-fill to last column of data and then to last row of data

    See below

    Sub Sans()
    'dim statements to define variable type. save on memory usage
        Dim Lrow As Long, Lcol As Long
        'Last row statement. Rows.Count is the last row in the sheet.
        'End(xlUp)looks from the Rows.Count backwards until it finds the last row with data.
        '.Row turns the cell to a row number i.e row 43
        ' The minus 2 takes 2 from the row number i.e. row 41. THIS IS THE NUMBER YOU NEED TO CHANGE.
        'In your last example change it to 6
        Lrow = Cells(Rows.Count, 12).End(xlUp).Row - 2
        'Similar to the row explanation. The 1 is the row number.
        Lcol = Cells(1, Columns.Count).End(xlToLeft).Column
        ' ranges just the same as a normal range. The 4 is the starting row number, Column "L" is Column 12.
        Range("L4").AutoFill Destination:=Range(Cells(4, 12), Cells(4, Lcol)), Type:=xlFillDefault
        'As above
        Range(Cells(4, 12), Cells(4, Lcol)).AutoFill Destination:=Range(Cells(4, 12), Cells(Lrow, Lcol)), Type:=xlFillDefault
    End Sub
    Or I suppose another option is to change
    Lrow = Cells(Rows.Count, 12).End(xlUp).Row - 2
    to
    Lrow = Columns(12).Find(what:="*", after:=Cells(4, 12), LookIn:=xlValues).Row - 1
    But please note you will have to clear the contents each time before running.
    Last edited by WasWodge; 03-31-2013 at 08:14 AM. Reason: Colour

  5. #5
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Auto-fill to last column of data and then to last row of data

    Worked like a charm!
    Clearing the contents each time before running the code seems obvious but obviously I didn't think of it before you mentioned it! Thank you very much for the help and the explanation WasWodge. Really appreciated!

  6. #6
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Auto-fill to last column of data and then to last row of data

    Just to point out the clearing of data only applies if you are using the line below as it needs the empty spaces to work
    Lrow = Columns(12).Find(what:="*", after:=Cells(4, 12), LookIn:=xlValues).Row - 1
    If you are using something like the code below then it doesn't matter (unless you have less columns) as it doesn't need the empty spaces, because it looks for the last row with data and not the first row with data.
    Lrow = Cells(Rows.Count, 12).End(xlUp).Row - 6
    Last edited by WasWodge; 03-31-2013 at 08:03 PM. Reason: added a space

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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