VBA hide columns if

eliozo

Board Regular
Joined
Oct 22, 2010
Messages
80
I want to click a button where I need to type a date, then automatically hide all columns where the date typed is greater than the dates in Rows 11 in my workbook.Can you please help asap?Thank you so much
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Please try this
Code:
Sub HideColumnsOnDate()
  
  Dim v As Variant
  Dim CelDate As Date
  Dim Def As String
  Dim iDate As Date
  Dim R As Range
  Dim Cel As Range
  Dim unhide As Range
  Dim hide As Range
  
  Def = Format(Now(), "mm/dd/yyyy")
  v = Application.InputBox("Please Enter a date", "Enter a Date", Def, Type:=1)
  If IsError(v) Then Exit Sub
  If v = "" Then Exit Sub
  iDate = Int(v)
  If IsDate(iDate) = False Then Exit Sub
  
  Set R = Range(Range("A11"), Range("ZZ11"))
  For Each Cel In R
    CelDate = Int(Cel.Value)
    If Cel.Value <> "" And Cel.Value > 0 And IsDate(CelDate) Then
      If iDate > CelDate Then
        If Not hide Is Nothing Then
          Set hide = Union(hide, Cel)
        Else
          Set hide = Cel
        End If
      Else
        If Not unhide Is Nothing Then
          Set unhide = Union(unhide, Cel)
        Else
          Set unhide = Cel
        End If
      End If
    End If
  Next Cel
  If Not hide Is Nothing Then
    hide.EntireColumn.Hidden = True
  End If
  If Not unhide Is Nothing Then
    unhide.EntireColumn.Hidden = False
  End If
  
  
  
End Sub
 
Upvote 0
CelDate = Int(Cel.Value)


It gave me an error on this line.
Thank you so much for helping me but if you can fix this bug. Plus I need a button to unhide all the columns that were hided. If possible to do it on the same button, if not it's ok another button might help too. Thank you so much for replying asap.
 
Upvote 0
See how this goes. If you want to unhide all the columns, leave the input box empty and just click OK.
Code:
Sub Hide_Cols()
  Dim Dte As Date
  Dim sDte
  Dim c As Range
  
  sDte = InputBox("Enter date")
  Application.ScreenUpdating = False
  If Len(sDte) = 0 Then
    ActiveSheet.UsedRange.EntireColumn.Hidden = False
  ElseIf IsDate(sDte) Then
    ActiveSheet.UsedRange.EntireColumn.Hidden = False
    Dte = DateValue(sDte)
    For Each c In Intersect(Rows(11), ActiveSheet.UsedRange)
      If IsDate(c.Value) Then c.EntireColumn.Hidden = Dte > c.Value
    Next c
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top