voorbeeld-2-july-updated.xlsHi Kreppie,
You can find some code below.
I have attached your updated file as well.
So the function / macro "deleteColumns" will delete the columns, you cannot restore that. So in case you have the next month, you need to update your sheet again.
Maybe more convienent to just hide/unhide the columns? Then you can use the other macro "hideUnhideColumns"
If you check my returned workbook, the "thisWorkbook" object refers to this macro on startup. With other words: If you open your file, it checks what columns need to be displayed.
Problem solved?
Sub deleteColumns()
Dim r As Range
Dim rFound As Range
Dim d As String
d = Day(Date) 'dateformat
Set r = ActiveSheet.Range("E5:AI5")
'Find the column with the day number
Set rFound = r.Find(what:=d, lookat:=xlWhole)
'Protect in case something is already deleted
If rFound Is Nothing Then
MsgBox "Day not found, or already deleted?"
Exit Sub
End If
'Delete from column D until the column found with the day number (minus offset 1 day/column)
ActiveSheet.Range("D1:" & rFound.Offset(0, -1).Address).EntireColumn.Delete
'Deselect
ActiveSheet.Range("A1").Select
End Sub
Sub hideUnhideColumns()
Dim r As Range
Dim rFound As Range
Dim d As String
d = Day(Date) 'dateformat
Set r = ActiveSheet.Range("D5:AI5")
'activate all columns first
r.Columns.EntireColumn.Hidden = False
'Find the column with the day number
Set rFound = r.Find(what:=d, lookat:=xlWhole)
'Protect in case something is already deleted
If rFound Is Nothing Then
MsgBox "Day not found, or is it deleted?"
Exit Sub
End If
'Delete from column D until the column found with the day number (minus offset 1 day/column)
ActiveSheet.Range("D1:" & rFound.Offset(0, -1).Address).EntireColumn.Hidden = True
'Deselect
ActiveSheet.Range("A1").Select
End Sub
Bookmarks