+ Reply to Thread
Results 1 to 6 of 6

insert VARYING blank column with two intervals,

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    insert VARYING blank column with two intervals,

    Hi Guys
    I want to insert columns into my sheet. It has some columns filed say 10. I have a VBA formula which inserts one column. I cant work out how to alter it to add say 3 blank columns. I know that the 30 will need to be increased to allow for more columns and it would be sweet if this was automatic. Have not loaded an example but if you want one please ask. I got this code from "http://www.extendoffice.com/documents/excel/798-excel-insert-blank-column-every-other-column.html"

    Range("A1").Select
                For colx = 2 To 30 Step 2
                Columns(colx).Insert Shift:=xlToRight
        Next
    Any suggestions
    Mark

  2. #2
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: insert VARYING blank column with two intervals,

    Hey,

    Hope this will help you.....

    Sub InsertColumns()
        
        Dim Ctr As Integer
        Dim ColNumber As Integer
    
    'You can put ColNumber as numric or as well text like a,b,c,d,e
            ColNumber = 5
            
    '3 means it will add 3 columns in Columns(5)=Columns(E)
            For Ctr = 1 To 3
                Columns(ColNumber).Insert Shift:=xlToRight
            Next Ctr
            
    End Sub
    Lokesh Kumar
    Stay Hungry.. Stay Foolish..
    _________________________________________________________
    Please Click STAR to Add Reputation if my/someone's answer helped!

  3. #3
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Re: insert VARYING blank column with two intervals,

    Hi Lokesh
    Hope all is well with you in India. I am in London and today the sun is shining but my office is in my basement. I appreciate your help. Thank you.

    You variation works great but is not precisely what I am looking for. I have a varying number of entered columns (say 10) and I want to insert blank columns starting at column B. Your VBA inserts only once not to the end of the entered columns.
    Note the existing example will insert one column for as many columns as are full.
    Does this make sense or should I upload and example?

    Mark

  4. #4
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: insert VARYING blank column with two intervals,

    Hi Mark
    I am good too...
    I just paste the code for your understanding. Try the following code....see it works for you or not?

    Option Explicit
    Sub InsertColumns()
        
        Dim Ctr As Integer
        Dim ColInsert As Integer
        Dim ColName As String
        Dim ColPos As Variant
        Dim RunAgain As Integer
    
            
            Application.ScreenUpdating = False
                Do
    StartPoint1:
                    ColName = InputBox("Enter the Column Name.", "Insert Columns", "A")
                    
                        If IsNumeric(ColName) Then
                            MsgBox "Column Name should be Text only" & vbNewLine & "For example A, B, C Etc.", vbCritical
                            GoTo StartPoint1
                        End If
    StartPoint2:
                    ColPos = InputBox("Enter the Column Position (Right,Left).", "Insert Columns", "Right")
                        
                        If LCase(ColPos) = LCase("Right") Or LCase(ColPos) = LCase("Left") Then
                        
                        Else
                            MsgBox "Column Position should be Left/Right!", vbCritical
                            GoTo StartPoint2
                        End If
                        ColPos = "xlTo" & ColPos
                    
    StartPoint3:
                    ColInsert = InputBox("How many columns need to be inserted?", "Insert Columns", 2)
                        
                        If Not IsNumeric(ColInsert) Then
                            MsgBox "It should be numeric number only!", vbCritical
                            GoTo StartPoint3
                        End If
            
            Application.ScreenUpdating = True
            
                    For Ctr = 1 To ColInsert
                        
                        If WorksheetFunction.Proper(ColPos) = "Left" Then
                            Columns(ColName).Offset(0, 1).Insert Shift:=xlToRight
                        Else
                            Columns(ColName).Offset(0, 1).Insert Shift:=xlToLeft
                        End If
                        
                    
                    Next Ctr
                    
                    RunAgain = MsgBox("Do you want to insert more column/s?", vbYesNo, "Excel")
                
                Loop Until RunAgain = vbNo
    
    End Sub
    Here is the attachment..
    Mark.xlsm

    Thanks
    Lokesh K.
    Last edited by LokeshKumar; 04-10-2015 at 02:07 PM.

  5. #5
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Re: insert VARYING blank column with two intervals,

    Hi Lokesh

    The information you have very kindly sent me is excellent. however I have not explained correctly what I am looking for. The only difference is that I would like to be able to stipulate the number of blanks and then for the program to run till there were no more entered columns to insert between. I may have up to 100 columns between which i want to add a certain number of blanks columns. I have copied a macro into the file you made and am uploading it again. You will see what I mean. In the mean time i will go back to studying VBA. I have an awful lot to learn to be even close to these on this forum.

    Mark
    Attached Files Attached Files

  6. #6
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: insert VARYING blank column with two intervals,

    I got it your code and it is working correctly.....however you need to declare colx as integer/long...
    Last edited by LokeshKumar; 04-14-2015 at 04:06 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. [SOLVED] Varying price points based on hour intervals
    By tylkum in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-19-2014, 10:16 AM
  2. [SOLVED] Compare 4 column values then insert a 5th columns value to a blank column/cell value
    By JasonKMcCoy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-27-2012, 06:01 PM
  3. Replies: 5
    Last Post: 10-13-2012, 06:26 AM
  4. Varying column width and x intervals (bar graph)
    By ereed20 in forum Excel General
    Replies: 2
    Last Post: 06-08-2011, 12:04 PM
  5. insert a blank column if a cell is blank...how?
    By bego in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2007, 03:04 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