SOLVED

Hide collums that are before the current date

Occasional Contributor

Hello,

 

I wanted to hide the collumns(G2:NG2) that came before the date of today(D2).

So for example, today 11/04/2022. All the dates before that should be hidden

Screenshot 2022-04-11 154830.png

But so far I got this:

Dim rng As Range

Set rng = ActiveSheet.Range("G2:NG2")

For Each cell In rng
    If Month(cell) = Month(Date) Then
        Columns(cell.Column).Hidden = True
    End If
Next cell

But it doesn't work the way I want.

1 Reply
best response confirmed by GKE2019 (Occasional Contributor)
Solution

@GKE2019 

Like this:

    Dim rng As Range
    Dim cell As Range
    Application.ScreenUpdating = False
    Set rng = ActiveSheet.Range("G2:NG2")
    For Each cell In rng
        If cell.Value < Date Then
            cell.EntireColumn.Hidden = True
        Else
            Exit For
        End If
    Next cell
    Application.ScreenUpdating = True