Insert column sub not working

vba317

Board Regular
Joined
Oct 7, 2015
Messages
58
I have written a sub to look for columns that are missing according to a spec. If the column is missing insert a new column is according to spec. If the column exists but is in the wrong column move it to where it should be.
The issue I am having is if the column exists but has to be moved it is moved into the wrong column. This is happening because I insert a new column where the column should be. Because of this insertion my column count is off, but I don't know how to add a column to a variable. Any help is appreciated.

Code:
Public Sub InsertNewColumns(sName As String, sMoveCol As String, sCol As String)
Dim lNewColumn As Long
Dim sNewColumn As String
Dim lLastCol As Long


    With Sheets("Data")
         If sCol <> "" Then
            .Range(sCol & "1").Value = sName
            'Insert new  Column
            .Columns(sMoveCol).Insert Shift:=xlToRight
            'Cut and paste existing data and move it to new Column 
            .Columns(sCol).Cut .Columns(sMoveCol)
            'Delete Old empty column
            .Columns(sCol).EntireColumn.Delete
         Else
            .Columns(sMoveCol).Insert Shift:=xlToRight
            .Range(sMoveCol & "1").Value = sName
        End If
        If .Range("B1").Value = "Ulanme" Then .Range("B1").Value = "LastName"
        If .Range("C1").Value = "Ufname" Then .Range("C1").Value = "FirstName"
    End With
End Sub


[\code]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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