Forum Discussion

GKE2019's avatar
GKE2019
Brass Contributor
Apr 11, 2022
Solved

Hide collums that are before the current date

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

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.

  • 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

1 Reply

  • 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

Resources