+ Reply to Thread
Results 1 to 13 of 13

Insert Column after Last Inserted Column

Hybrid View

mariosmk555 Insert Column after Last... 05-04-2014, 06:25 AM
HaHoBe Re: Insert Column after Last... 05-04-2014, 06:43 AM
mariosmk555 Re: Insert Column after Last... 05-04-2014, 07:39 AM
HaHoBe Re: Insert Column after Last... 05-04-2014, 07:47 AM
mariosmk555 Re: Insert Column after Last... 05-04-2014, 07:55 AM
sktneer Re: Insert Column after Last... 05-04-2014, 08:36 AM
mariosmk555 Re: Insert Column after Last... 05-04-2014, 08:51 AM
  1. #1
    Registered User
    Join Date
    03-21-2014
    Location
    Russia, Moscow
    MS-Off Ver
    Excel 2013
    Posts
    39

    Insert Column after Last Inserted Column

    A simple macro:

    1.Run macro: Add a column after A (I have data at column B)
    -->Now column A:same, Column B: new blank, column C:previous column B
    2.Run macro again: Add a column after B
    ...
    3.Run macro again: Add a column after C
    ...
    Last edited by mariosmk555; 05-04-2014 at 06:31 AM.

  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: Insert Column after Last Inserted Column

    Hi, mariosmk555,

    maybe like this:
    Sub EF1008505()
    Cells(1, Columns.Count).End(xlToLeft).EntireColumn.Insert   'adjust the row number to match data
    End Sub
    or
    Sub EF1008505_2()
    With ActiveSheet.UsedRange
      .Cells(.Cells.Count).EntireColumn.Insert
    End With
    End Sub
    Ciao,
    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

  3. #3
    Registered User
    Join Date
    03-21-2014
    Location
    Russia, Moscow
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Insert Column after Last Inserted Column

    Quote Originally Posted by HaHoBe View Post
    Hi, mariosmk555,

    maybe like this:
    Sub EF1008505()
    Cells(1, Columns.Count).End(xlToLeft).EntireColumn.Insert   'adjust the row number to match data
    End Sub
    or
    Sub EF1008505_2()
    With ActiveSheet.UsedRange
      .Cells(.Cells.Count).EntireColumn.Insert
    End With
    End Sub
    Ciao,
    Holger
    Hello and thanks. How can I set the first column tha the macro will start from? Lets say C.
    Thanks again,

  4. #4
    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: Insert Column after Last Inserted Column

    Hi, mariosmk555,

    no need for a full quote here according to Forum Rule#12.

    Only if Data exceeds Column C it would be of use here:
    Sub EF1008505_3()
    Dim lngIns As Long
    lngIns = Cells(1, Columns.Count).End(xlToLeft).Column
    If lngIns < 3 Then lngIns = 3
    Cells(1, lngIns).EntireColumn.Insert   'adjust the row number to match data
    End Sub
    Starting at a certain range will not insert new columns prior to any filled cells, that´s a different requirement.

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    03-21-2014
    Location
    Russia, Moscow
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Insert Column after Last Inserted Column

    Quote Originally Posted by HaHoBe View Post
    Hi, mariosmk555,


    Ciao,
    Holger
    Thanks for the fast response,
    maybe I didn't explain well my goal.

    I want to set a column: for example D (D and E are filled with data)
    I want to run macro: and create 2 columns after D and shift E two times right.
    When I run again the macro I want the new columns to be created after the previous two columns that macro created.
    etc..

    **Today is my first day with VBA and excel macro!

  6. #6
    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: Insert Column after Last Inserted Column

    Hi, mariosmk555,

    you should be more precise in what you are asking. In the opening post it was one column (the last one) which should be shifted. Now it´s two columns, and I´m not so sure about the columns you have used or the range where to insert:
    Sub EF1008505_4()
    'insert two columns before the last two columns of data
    Dim lngIns As Long
    lngIns = Cells(1, Columns.Count).End(xlToLeft).Column - 1
    If lngIns < 3 Then lngIns = 3
    Cells(1, lngIns).Resize(1, 2).EntireColumn.Insert
    End Sub
    Sub EF1008505_5()
    'will start at Column C and add two columns when the workbook isn´t closed in the meantime
    'If you wan to keep the value for the next opening you could write it to a cell on a hidden sheet,
    'use a Name to store it or store the value in a textfile.
    Static lngIns As Long
    If lngIns = 0 Then
      lngIns = 3
    Else
      lngIns = lngIns + 2
    End If
    Cells(1, lngIns).Resize(1, 2).EntireColumn.Insert
    End Sub
    Ciao,
    Holger

  7. #7
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Insert Column after Last Inserted Column

    You may try this code also.....
    The code will ask you to input the column of interest, so you need to input the column alphabet. Suppose you want to insert two column after the col. D, in that case you need to input either d or D in the input box.
    Sub InsertColumn()
    Dim c As String
    'You need to input the column alphabet like d,e,f,g...etc
    c = InputBox("After which column you want to insert new column?")
    Cells(1, c).Offset(0, 1).EntireColumn.Insert
    Cells(1, c).Offset(0, 1).EntireColumn.Insert
    End Sub
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  8. #8
    Registered User
    Join Date
    03-21-2014
    Location
    Russia, Moscow
    MS-Off Ver
    Excel 2013
    Posts
    39

    Re: Insert Column after Last Inserted Column

    Quote Originally Posted by sktneer View Post
    You may try this code also.....
    The code will ask you to input the column of interest, so you need to input the column alphabet. Suppose you want to insert two column after the col. D, in that case you need to input either d or D in the input box.
    Sub InsertColumn()
    Dim c As String
    'You need to input the column alphabet like d,e,f,g...etc
    c = InputBox("After which column you want to insert new column?")
    Cells(1, c).Offset(0, 1).EntireColumn.Insert
    Cells(1, c).Offset(0, 1).EntireColumn.Insert
    End Sub
    Good idea but instead of giving the option to the user to choose the column,

    (I repeat what I said above), if we give a name to the column from which the new columns will always be created before it will be easier. Can we do that in macro?

    paint.jpg
    Last edited by mariosmk555; 05-04-2014 at 08:55 AM.

+ 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. Add a checkbox to column whenever a new row is inserted
    By Phillips Contracting in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2014, 01:44 PM
  2. Copy column sub-headings to inserted column via formula
    By Geppstar in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-13-2013, 05:36 PM
  3. Sum of columns to right - update when new column inserted
    By miro2021 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-21-2013, 11:00 AM
  4. Insert column and fill column upto where data is in previous column
    By aka189 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-24-2012, 06:07 AM
  5. [SOLVED] AUTO INSERT A COLUMN'S FORMULA IN MANUALLY INSERTED ROWS
    By JLah in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-20-2006, 11:15 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