VBA Shifting Columns To Left

cgmojoco

Well-known Member
Joined
Jan 15, 2005
Messages
699
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)
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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