+ Reply to Thread
Results 1 to 6 of 6

Macro to Insert two columns to the left of a moving total column

  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    Stratford, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Macro to Insert two columns to the left of a moving total column

    Hi, I'm new to VBA coding and I've been trying to create a macro to insert two new columns to the left of a "Total" column that continues to grow to the right as new columns are added. I'd like the insert to be initiated by a button. My attempt at recording a macro failed as my new columns seem to go to a static position. I copied two columns that I would like to use as my format and pasted them to the left of the "Total" column. I can't seem to get the macro to find the end column and go back one to insert the new column. When I run the macro, it overwrites my previous insertion. Any help would be greatly appreciated.

    Thanks

    JM

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Macro to Insert two columns to the left of a moving total column

    Assuming the "Totals" column is the last column and that Row A has a value in the last column.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-16-2013
    Location
    Stratford, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Macro to Insert two columns to the left of a moving total column

    Insert Coloumns.xlsx

    Thanks for the reply. That didn't quite do what I was hoping. Attached is a basic idea of what I would like the worksheet
    to look like before and after the macro has run.

    Thanks,

    JM

  4. #4
    Registered User
    Join Date
    07-16-2013
    Location
    Stratford, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Macro to Insert two columns to the left of a moving total column

    Sub Insert()

    Columns("H:I").Select
    Selection.Copy
    Range("IV2").End(xlToLeft).Offset(0, -1).EntireColumn.Insert
    Range("IV2").End(xlToLeft).Offset(0, -1).Select

    End Sub

    This seems to be close to what I'm looking for

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Macro to Insert two columns to the left of a moving total column

    Your workbook has merged cells which causes a significant problem for macros. You will need to remove them. Try this:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    07-16-2013
    Location
    Stratford, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Macro to Insert two columns to the left of a moving total column

    Thanks, I used your date code and incorporated it with my attempt. This worked nicely.

    Sub Insert()

    Range("InsertColumns").Select
    Selection.Copy
    Range("IV2").End(xlToLeft).Offset(0, -1).EntireColumn.Insert
    Cells(1, Columns.Count).End(xlToLeft).Value = Date
    Range("IV2").End(xlToLeft).Offset(0, -1).Select

    End Sub

+ 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. Macro to insert n columns every nth column
    By kreese in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-03-2013, 05:34 PM
  2. Creating a macro to insert a blank row and total sums in a new column
    By volleyballtigger in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-23-2013, 10:42 AM
  3. Tried to insert a Scroll bar and now my Columns go right to left
    By Cidona in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 08-26-2010, 11:51 AM
  4. Macro to search column with last value and insert TOTAL column to the left
    By sbara906 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-02-2009, 11:34 AM
  5. Right column missing; vertical dotted line is moving left
    By NorthernLights in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 06-11-2006, 01:35 AM

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