Vba to hide columns based on todays date

Krs1527

New Member
Joined
Jan 15, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Can anyone suggest a vba to hide columns based on todays date. I have 12 months of data but only want to show current month, last 3 months or last 12 months based on checkbox
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi Krs1527,

Welcome to board.


I would suggest providing a bit more information in regards to the structure of your worksheet/data. That would make it easier for everyone to suggest solutions rather then working on assumptions.

Anyway, assuming you have dates in 1st row from column B to D as (1-1-21, 1-2-21, 3-1-21), try the following

VBA Code:
Sub hide_Col()
Dim rng As Range

Set rng = ActiveSheet.Range("b1:d1")

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

End Sub


hth...
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,778
Members
449,336
Latest member
p17tootie

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