Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Custom Format uknown number of column headers to mmm-yy Excel 2016 VBA

Hi all.

I'd like to custom format to mmm-yy the column headers starting with H1 and going on to I1, J1, K1, L1 etc. as long as the cells in each column header have data.

I don't know how many columns will be in the spreadsheet beforehand as it is all based on the end users parameters. Sometimes there may be just 2 column headers H1 and I1, other times there might be 30 etc.

Thank you in advance!
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi,

Try below:
Sub ChangeFormat()
Dim Ws As Worksheet
Dim LC As Integer
Dim i As Long
Application.ScreenUpdating = False
Set Ws = ActiveSheet
LC = Ws.Cells(1, Ws.Columns.Count).End(xlToLeft).Column
For i = 1 To LC
    Ws.Cells(1, i).NumberFormat = "mmm-yy"
Next i
Application.ScreenUpdating = True
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of printmedia
printmedia

ASKER

Thanks Shums! And if I wanted to start in column H then can I change the For loop to start at 8?

For i = 8 To LC
    If IsDate(Ws.Cells(1, i)) Then
        Ws.Cells(1, i).NumberFormat = "mmm-yy"
    End If
Next i

Open in new window

Yes Sir that's right
Thanks!
Pleased to help