Hi, Im working on inserting columns based on cell value on the header row.
My bit of code (below) it is supposed to iterate over all columns header row and every instance it finds "YEAR" insert a column.
However it does not work as I intended and it inserts a big number of columns before the first occurrence of "YEAR" and then stops.
Can someone please explain why? Im more interested in understanding why this not work as I intended. Much thanks in advance.
My bit of code (below) it is supposed to iterate over all columns header row and every instance it finds "YEAR" insert a column.
However it does not work as I intended and it inserts a big number of columns before the first occurrence of "YEAR" and then stops.
Can someone please explain why? Im more interested in understanding why this not work as I intended. Much thanks in advance.
VBA Code:
Sub ColumnInsert()
Dim k As Integer
LC = Cells(1, Columns.Count).End(xlToLeft).Column
For k = 2 To LC
If Cells(1, k).Value = "Year" Then
Cells(1, k).EntireColumn.Insert
End If
Next k
End Sub