Inserting columns to the left or to the right

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
I am trying to have a macro to insert columns, and I would like to be in control of whether the new column is inserted to the left or to the right of a certain column. Consider these two small macros:

Code:
Sub InsertBeforeC()
    Columns("C:C").Select
    Selection.Insert Shift:=xlToLeft, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Sub InsertAfterC()
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub

Regardless of which of these two macros I use, the new column is inserted to the left of C. I had hoped that the InsertBefore macro should insert a column to the left of C and the InsertAfter macro should insert a column to the right of C, but that doesn't happen. What do I do wrong?

Bengt
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Bengt

Maybe write your own subroutine for this, e.g:
Code:
Public Sub NewColumn(ByVal lngShift As XlDirection)
    If lngShift = xlToLeft Then
        Selection.EntireColumn.Insert
    ElseIf lngShift = xlToRight Then
        Selection.Offset(, 1).EntireColumn.Insert
    End If
End Sub

Use as follows:
Code:
Sub test()
    NewColumn xlToRight
End Sub
 
Upvote 0
Hi Bengt

Maybe write your own subroutine for this, e.g:
Code:
Public Sub NewColumn(ByVal lngShift As XlDirection)
    If lngShift = xlToLeft Then
        Selection.EntireColumn.Insert
    ElseIf lngShift = xlToRight Then
        Selection.Offset(, 1).EntireColumn.Insert
    End If
End Sub

Use as follows:
Code:
Sub test()
    NewColumn xlToRight
End Sub

Thanks for your help Jon, I'll try it out. Still, my original question remains: Why have something called "xlToRight" or "xlToLeft" if columns aren't inserted to the right or to the left respectively, but only to the left?

Bengt
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top