Home > Enterprise >  hiding rows with If statement for a calander
hiding rows with If statement for a calander

Time:10-11

So I am a student and my chef gave me the task of making the occupancy plan in excel more dynamic. Now I want to hide the columns if they dont belong to the specific month but it doesnt work like I want it. The days are now only shown up to the 28. of every month. What is wrong here?

Sub montatslen()

Dim spaltenr As Integer

For spaltenr = 32 To 34
    If Cells(3, 7).Value = Month(Cells(14, spaltenr).Value) Then
        Columns(spaltenr).EntireColumn.Hidden = False
    Else
        Columns(spaltenr).EntireColumn.Hidden = True
    End If
Next spaltenr

End Sub

CodePudding user response:

I tried writing it this way and it seemed to work. It hides columns AF, AG and AH

Sub montatslen()

Dim spaltenr As Integer

For spaltenr = 32 To 34
    
    columnLetter = Split(Cells(1, spaltenr).Address, "$")(1)
    
    If Range("G3").Value = Month(Range(columnLetter & "14").Value) Then
        Columns(spaltenr).EntireColumn.Hidden = False
    Else
        Columns(spaltenr).EntireColumn.Hidden = True
    End If
Next spaltenr

End Sub
  • Related