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.
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]