Forum Discussion
Nicarlov
Jul 01, 2019Copper Contributor
Excel Column Management
I am not an Excel guru, but I do have an Excel2013 book by Microsoft. I have a budget in Excel and the columns are identified by month. I would like to automatically hide the previous months and di...
Smitty Smith
Jul 01, 2019Former Employee
Nicarlov You can add this code to the the ThisWorkbook module:
Private Sub Workbook_Open()
Dim i As Long
Dim strMonth As String
strMonth = Format(Date, "mmmm")
'Debug.Print strMonth
' Unhide all month columns
Range("B1:M1").EntireColumn.Hidden = False
' Loop through each column - If it's <> to this month, then hide it
For i = 2 To 13
If Cells(1, i).Value <> strMonth Then
Cells(1, i).EntireColumn.Hidden = True
End If
Next i
End Sub
If you're not familiar with VBA code, then press Alt+F11 to open the VB Editor. Next, find the module called ThisWorkbook, double-click it, then paste the code into the new window that opens on the right. You can test it by pressing F5. Alt+Q will exit you back to Excel, and the code will run each time you open the workbook.
HTH