VBA Shifting Columns To Left

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
696
This snippet of a VBA routine is meant to insert a new column to the right of the selected column:

Code:
Sub NewColumn()
Dim colPAY As Integer
   colPAY = Application.Match("Payment Source", Range("1:1"), 0)
   Columns(colPAY).Insert Shift:=[B][COLOR=red]xlToLeft[/COLOR][/B]
End Sub

the xlToLeft seems to do the same thing as xlToRight.

Is there a simple way of inserting a column to the right of the selected column rather than the left other than in this case adding 1 to the colPAY integer like this?

Code:
Sub NewColumn()
Dim colPAY As Integer
   colPAY = Application.Match("Payment Source", Range("1:1"), 0) [COLOR=red][B]+ 1
[/B][/COLOR]   Columns(colPAY).Insert Shift:=[B][COLOR=#ff0000]xlToRight[/COLOR][/B]
End Sub

The reason I am looking for this solution is that I find myself having to reset the colPAY integer to the actual column if I want to refer to the exact column anywhere later in my code and still have it make sense:
Code:
 colPAY = Application.Match("Payment Source", Range("1:1"), 0)
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Watch MrExcel Video

Forum statistics

Threads
1,123,489
Messages
5,601,982
Members
414,489
Latest member
Xlambda

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
Top