+ Reply to Thread
Results 1 to 11 of 11

Inserting columns to an existing sheet but alphabetically

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Inserting columns to an existing sheet but alphabetically

    HI,
    I need to insert a 2 new Columns into an existing worksheet but they have to fit in the correct place alphabetically.
    I've got an input box that get the new Column's Title, but at the moment I can only add it to the next blank column at the end.
    I need code that will take the input box text and check it against all the colums then insert a new colum in the right aphabetical place.

    Here's the code I'm using which adds two columns at the end and formats them correctly.

    Sub Add_LB()
    'Find Last Column
    Dim LastCol As Integer
        With ActiveSheet
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        End With
    'Add Collumn Title
    Dim myValue As Variant
    myValue = InputBox("Enter title of new Learning Burst")
    If myValue = "" Then GoTo Skip:
    Cells(1, LastCol + 1).Select
    ActiveCell.FormulaR1C1 = myValue
        
        With ActiveCell
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 90
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Font.Bold = True
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.499984740745262
            .PatternTintAndShade = 0
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
        End With
        
    ' Add Follow Up Text in next column
    Cells(1, LastCol + 2).Select
    ActiveCell.FormulaR1C1 = "Follow Up"
    Cells(1, LastCol + 2).Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 90
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            Selection.Font.Bold = False
        End With
    Skip:
    End Sub

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Inserting columns to an existing sheet but alphabetically

    why don't you sort all of your columns after having inserted your new columns? I previously used that in a macro and it works.
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Inserting columns to an existing sheet but alphabetically

    Quote Originally Posted by p24leclerc View Post
    why don't you sort all of your columns after having inserted your new columns? I previously used that in a macro and it works.
    Thanks for replying. I thought of that but I couldn't work out how to do it :-( Any ideas Please?

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Inserting columns to an existing sheet but alphabetically

    in your code, you are adding one column with a title you get from an Input box and one which title is fixed ("Follow up").
    Are the Follow up columns related to the column with the titles got from Inputbox?
    Do you have many Follow up columns? If so, sorting all of your column will place all of those Follow up columns one after the other.
    What exactly do you want to do?
    Can you attached your workbook so I can work on your actual data?

  5. #5
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Inserting columns to an existing sheet but alphabetically

    I'll be able to get the workbook for you to look at on Tuesday 29th Thanks.

  6. #6
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Inserting columns to an existing sheet but alphabetically

    Thanks that's a good point; even if I did sort them they would sort wrongly as you say.
    I need to take the title from the input box, check where it would fit alphabetically, insert it, insert another column directly after that called "Follow Up".
    so it would look something like this to start off with:

    A Title Follow UP B Title Follow UP D Title Follow Up

    Imputing "C Title" as the new column title would insert it after "B Title Follow Up" to give:

    A Title Follow UP B Title Follow UP C Title Follow Up D Title Follow Up

    I need some VBA code that checks the New Column Title against all the existing Column Titles and inserts the two new columns in the correct place.

    I hope that makes sense?

    I really appreciate your help.

  7. #7
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Inserting columns to an existing sheet but alphabetically

    I would rather use 2 rows for the titles. The input box result would go in row 1 of both columns then the second row would have some sub-titles like "Data" and "Follow up".
    That way, the automatic sort will work out just as you want.

  8. #8
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Inserting columns to an existing sheet but alphabetically

    Cheers, that's a good idea. I'll give it a try on Tuesday, when I'm back in my office.

  9. #9
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Inserting columns to an existing sheet but alphabetically

    Hi, Sorry for the delay. Here's my workbook so far.
    Attached Files Attached Files

  10. #10
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Inserting columns to an existing sheet but alphabetically

    See the modifications made to your Main sheet in the attached workbook.

    The following macro will sort you columns:
    'SORT the columns
    Last_row = Cells(Rows.Count, 5).End(xlUp).Row 'Row of last formula in column 5 is the number of rows to sort
        Range("F1", Cells(1, Columns.Count).End(xlToLeft)).EntireColumn.Select
        ActiveWorkbook.Worksheets("Main").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Main").Sort.SortFields.Add Key:=Range("F1", Cells(1, Columns.Count).End(xlToLeft)), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Main").Sort
            .SetRange Range("F1", Cells(Last_row, Cells(1, Columns.Count).End(xlToLeft).Column))
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    Be sure to check carefully all of your formulas and other VBA codes as the added row might have impacted them.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Inserting columns to an existing sheet but alphabetically

    Thanks very much! That did the trick. I see what you mean about having sub headings. I need to alter a couple of formulas on the second sheet but that's easy. Thanks again.

+ 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. Inserting new columns and modifying existing VBA related code
    By kriminaal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2015, 05:23 PM
  2. [SOLVED] Inserting Blank Columns between existing non Blank columns
    By markDuffy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-22-2015, 01:27 PM
  3. Inserting rows between existing related data on another sheet
    By GDC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2013, 10:45 PM
  4. Creating a new xls sheet from a existing one with certain columns.
    By kirthi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2013, 11:09 AM
  5. Update few columns of information from another sheet to my existing report
    By Adamlearnexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2012, 02:01 AM
  6. inserting entire blank columns in pre-existing worksheets
    By sickly_man in forum Excel General
    Replies: 5
    Last Post: 07-09-2007, 09:50 AM
  7. Inserting on Line, Alphabetically
    By mpeplow in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-07-2007, 11:47 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