Hi,
I'm trying to write a macro that would insert new column everytime the value in the column changes. eg
AAABBBCCC would be AAA BBB CCC.
The following code doesn't seem to work correctly. Hopefully somebody can point out any errors on my code below.
Sub InsertColOnChange()
Dim Rw As Long
Dim Col As Long
Dim Endcol As Long
Dim C As Long
Application.ScreenUpdating = False
With Selection
If .Rows.Count > 1 Then
MsgBox "Please select only one row", vbCritical, "Selection error"
GoTo theend
End If
Rw = .Row
Col = .Cells(.Columns.Count, Rw).Column
Endcol = .Columns(1).Column
On Error GoTo theend
For C = Col To Endcol Step -1
If Cells(C, Rw) <> Cells(C - 1, Rw) Then
Cells(C, Rw).EntireColumn.Insert
End If
Next C
End With
theend:
Application.ScreenUpdating = True
End Sub
Bookmarks