+ Reply to Thread
Results 1 to 3 of 3

Autofilling the last column

Hybrid View

  1. #1
    Registered User
    Join Date
    04-11-2016
    Location
    Hereford, UK
    MS-Off Ver
    2013
    Posts
    2

    Autofilling the last column

    Hi,
    I'm new to VBA, self taught from what I've read online and from recording marcos.

    I have a spreadsheet that I need to insert a new column between the last column of data and the totals column.
    The date row is on row 5 so just find the last column in that row and it gives me that last column.

    That bit I can do.

    I then need to autofill the formulas across from last column into the new column. I'm struggling to get this bit to work.
    Below is what I have that works so far, I'm just now sure how to select the destination of the autofill.

    Sub NewMonth()
    
    Dim NewC As Long
    Dim LastC As Long
    
    'find last column
    
    LastC = Cells(5, Columns.Count).End(xlToLeft).Column
    
    'label the new column
    NewC = Cells(5, Columns.Count).End(xlToLeft).Column + 1
    
    'create new column
    Columns(NewC).Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFillDefault
    
    'copies the row across
    Columns(LastC).Select
    Selection.AutoFill Destination:=Columns(LastC & ":" & NewC), Type:=xlFillDefault
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    10-19-2012
    Location
    Omaha, Nebraska USA
    MS-Off Ver
    Excel 2010
    Posts
    249

    Re: Autofilling the last column

    Hi tom_wain90,

    Here is your code modified to do what you are looking for. I deleted the "NewC" variable and just used "LastC+1" instead. To copy the formulas without changing their cell references, I just used the ".formula" convention. In my code, I found the last row with data in the LastC column and then set the range to that lastrow because it runs a little faster. Otherwise, you can just copy column to column, but the code runs a little longer.

    Sub NewMonth()
    
    Dim LastC As Long
    Dim LastR As Long
    
    'find last column
    LastC = Cells(5, Columns.Count).End(xlToLeft).Column
    
    'create new column
    Columns(LastC + 1).Insert Shift:=xlToRight, CopyOrigin:=xlFillDefault
    
    'copies the row across
    ' Columns(LastC + 1).Formula = Columns(LastC).Formula     ---> This can be used instead of the lines below, but code runs a little slower
    LastR = Cells(Rows.Count, LastC).End(xlUp).Row
    Range(Cells(1, LastC + 1), Cells(LastR, LastC + 1)).Formula = Range(Cells(1, LastC), Cells(LastR, LastC)).Formula
    
    End Sub
    Hope this helps,

    Dan
    Last edited by djbomaha; 04-11-2016 at 12:49 PM. Reason: Added "End Sub" to code

  3. #3
    Registered User
    Join Date
    04-11-2016
    Location
    Hereford, UK
    MS-Off Ver
    2013
    Posts
    2

    Re: Autofilling the last column

    Hi Dan,
    Thanks for your reply, the reason I was opting for Autofill is I'm looking to change the cell reference. There's a monthly figures been shown on a summary page from umpteen worksheets so the cell references allow it to show figures from the relevant month.
    I've now got the AutoFill to work for one row but I couldn't get it to work for multiple rows, so using your last row feature I've managed to achieve that.
    Here's my code, it looks pretty busy it works haha.
    Sub NewMonth()
    
    If MsgBox("This add a new month to the sheet", vbYesNo) = vbNo Then Exit Sub
    
    Dim NewC As Long
    Dim LastC As Long
    Dim LastR As Long
    
    'find last column
    
    LastC = Cells(5, Columns.Count).End(xlToLeft).Column
    LastR = Cells(Rows.Count, LastC).End(xlUp).Row
    
    'label the new column
    NewC = Cells(5, Columns.Count).End(xlToLeft).Column + 1
    
    'create new column
    Columns(NewC).Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFillDefault
    
    'copies the row across
    
    Dim cellSource As Range
    Dim cellTarget As Range
    
    Set cellSource = Range(Cells(5, LastC), Cells(LastR, LastC))
    Set cellTarget = Range(Cells(5, LastC), Cells(LastR, LastC + 1))
    
    cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault
    
    
    End Sub
    Thanks again for your help
    Tom

+ 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] autofilling a column based on values on a row
    By goofer in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-01-2013, 04:01 PM
  2. [SOLVED] Macro for autofilling until the last row in another column.
    By Jongering in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2012, 07:32 AM
  3. Excel 2007 : turn off autofilling of column in 2007 table/list?
    By The Headacher in forum Excel General
    Replies: 2
    Last Post: 01-21-2010, 03:33 AM
  4. Autofilling Rows In A Column Using Existing Row Data
    By jessekanclerz in forum Excel General
    Replies: 4
    Last Post: 03-12-2009, 03:12 PM
  5. Autofilling with both column and row counting up
    By Robert78 in forum Excel General
    Replies: 1
    Last Post: 11-30-2006, 12:12 PM
  6. Autofilling down a column?
    By alann2 in forum Excel General
    Replies: 2
    Last Post: 08-15-2006, 11:16 AM
  7. [SOLVED] Autofilling next empty cell in column?
    By Bruce in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 04-20-2006, 11:55 PM
  8. Autofilling Date AND Time in Column
    By mcmitchell in forum Excel General
    Replies: 1
    Last Post: 01-13-2005, 03:04 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