+ Reply to Thread
Results 1 to 26 of 26

Hide Columns Older than Today

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Hide Columns Older than Today

    I have a simple spreadsheet and I'd like to hide columns with column header dates older than today.

    Column A has Names in each each A2 down
    Columns B through N = Dates

    Then data in cells B2

    Cell A1 has the Today formula =TODAY()

    10/5/2016 9/1/2016 9/8/2016 9/15/2016 9/22/2016 9/29/2016 10/6/2016 10/13/2016 10/20/2016 10/27/2016 11/3/2016 11/10/2016 11/17/2016 11/24/2016
    Fred
    Nancy
    Sam
    Jack
    Frank

    I need help with the macro to hide the columns and data that are older than today or compared to cell A1.

  2. #2
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: Hide Columns Older than Today

    Try this:

    Sub HideCols()
        Dim cell As Range
        Dim lcol As Integer
        lcol = ActiveSheet.UsedRange.Columns.Count
        For Each cell In Range(Cells(1, 1), Cells(1, lcol))
            If cell.Value < Date Then cell.EntireColumn.Hidden = True
        Next
    End Sub
    Regards,
    Stephen

    If you feel someone has helped you please thank them and click on the star on their post to add reputation.
    Please ensure that you use code tags where relevant, and mark solved threads as solved.
    Most of all please be respectful and understanding of others.

  3. #3
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    Thanks Stephen! Works pretty good!

    I don't want it to hide column A though, so how do I modify?'

    And, where do I put this code so that it automatically updates when the file/sheet is opened? Right now I've put it under a new module and manually running the module in the VBA application.

  4. #4
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    Thanks Stephen! Works pretty good!

    I don't want it to hide column A though, so how do I modify?'

    And, where do I put this code so that it automatically updates when the file/sheet is opened? Right now I've put it under a new module and manually running the module in the VBA application.

  5. #5
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: Hide Columns Older than Today

    Hi,

    This will start from column B
    Sub HideCols()
        Dim cell As Range
        Dim lcol As Integer
        lcol = ActiveSheet.UsedRange.Columns.Count
        For Each cell In Range(Cells(1, 2), Cells(1, lcol))
            If cell.Value < Date Then cell.EntireColumn.Hidden = True
        Next
    End Sub
    To start it when you open the workbook under ThisWorkbook use this:
    Private Sub Workbook_Open()
        Call HideCols
    End Sub

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    Why in a seperate module?
    Private Sub Workbook_Open()
        Dim i as long
        With Sheets("Sheet1") 'change to need
            For i = 2 To 14
                .Columns(i).Hidden = False
                If .Cells(1, i) < Date Then .Columns(i).Hidden = True
            Next
        End With
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    bakerman2,

    Your first code works great! However I'm still having issues with it running automatically when opening the sheet.

    In my test file newcode2.xls I've got some sample data, and under the Developer ribbon you can see the code now. If when viewing the VBA code I click the run icon it hides the columns perfectly.

    So, how do I make it hide the columns when opening the file?

    sample_data.PNG
    sample_vba.PNG

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Hide Columns Older than Today

    Private Sub Workbook_Open()
        Dim r As Range
        For Each r In Sheets("Sheet1").Cells(1).CurrentRegion.Offset(, 1).Rows(1).Cells
            r.EntireColumn.Hidden = (r <> "") * (r > Date)
        Next
    End Sub

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    @ jindon

    Also works nice but...

    Private Sub Workbook_Open()
        Dim r As Range
        For Each r In Sheets("Blad1").Cells(1).CurrentRegion.Offset(, 1).Rows(1).Cells
            r.EntireColumn.Hidden = (r <> "") * (r < Date)
        Next
    End Sub

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    You must put the code in the ThisWorkbook module, NOT a sheet module or a standard module

  11. #11
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    OK, so I was able to get it to hide as desired (columns older than today) and hide automatically upon opening the file. See screenshot.

    Now the requirement is to show only the next 10 periods. So hide before today, and for today through period 10, then hide all periods after.

    How can I specify to show only the next 10 columns?

    sample_vba_3.PNG

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    This should do it.
        Dim i As Long
        Application.ScreenUpdating = False
        With Activesheet 'change to need
            For i = 2 To .UsedRange.Columns.Count
                .Columns(i).Hidden = False
                If .Cells(1, i) < Date Or .Cells(1, i) > Date + 9 Then .Columns(i).Hidden = True
            Next
        End With
        Application.ScreenUpdating = True

  13. #13
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    That worked great! See attached screenshot of both codes used.

    Now I need to do the same for data in weekly buckets. Essentially we have the same report format but data in weekly buckets instead of days. I need to hide columns with weekly buckets older than this week, but show the next 10 (let's say to be consistent) weeks of data. How do I change from using days to weeks?

    Note: Refer to the first screenshot, not the one where I've attempted weeks. It won't let me delete the old incorrect image.

    sample_vba_5.PNG
    Attached Images Attached Images
    Last edited by ryanrickard; 10-11-2016 at 10:35 AM.

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    Please post example file with what your data looks like in Row1.

  15. #15
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    See attached screenshot of weekly data. Cell A1 has the today formula. That may need to be changed? It's from the previous daily report.

    sample_vba_weekly.PNG

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    If .Cells(1, i) < Date Or .Cells(1, i) > Date + 70 Then .Columns(i).Hidden = True
    If we use the above then only columns M to V would be visible. Is that what your after ?

  17. #17
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    Yeah, almost perfect. I was overthinking it, thinking of how to convert the formula to weeks.

    But, I need it to show current week also.

    Really appreciate your help bakerman2!

  18. #18
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    That complicates things a little because '.Cells(1, i) < Date' won't work anymore.
    In case of today's date (or for that matter all days of this week) you would like to see the column of 10/10/2016 also, yes?
    If that's the case we'll have to calculate which week today's date is in, calculate first day of that week (in case of today's date all through Sunday 16/10 would be 10/10) to include that column also.
    Does this sum it up a little ?

  19. #19
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    Yeah, I think the client may want to see this week also. At least if I can figure this out I'll have both options available (trying to think ahead).

    I don't have to use the =TODAY() formula in cell A1 if there's another way. I was planning to try to hide that cell anyway. How would I go about changing it up?

  20. #20
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    Paste below in your module and see if this gives you expected result.
    Don't forget to remove formula in A1 or column will also be hidden.

    Public Function ISOweeknum(ByVal v_Date As Date) As Integer
        ISOweeknum = DatePart("ww", v_Date - Weekday(v_Date, 2) + 4, 2, 2)
    End Function
    
    Public Function ISOday(v_year As Integer, v_week As Integer, v_day As Integer) As Long
        ISOday = 7 * (v_week - 1) + DateSerial(v_year, 1, 4) - Weekday(DateSerial(v_year, 1, 4), 2) + v_day
    End Function
    
    Sub HideCols()
        Dim i As Long, stDate As Date
        stDate = ISOday(Year(Date), ISOweeknum(Date), 1)
        Application.ScreenUpdating = False
        With ActiveSheet 'change to need
            For i = 2 To .UsedRange.Columns.Count
                .Columns(i).Hidden = False
                If .Cells(1, i) < stDate Or .Cells(1, i) > stDate + 65 Then .Columns(i).Hidden = True
            Next
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by bakerman2; 10-11-2016 at 12:46 PM.

  21. #21
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    bakerman2, that works perfectly! Thank you!

    I may be back for more help as I continue to tinker with this!

  22. #22
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    Glad to help.

  23. #23
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    Back to the daily one...Code that works is posted below...

    What happens if there is an additional set of data in Column B (let's say names or products) and the daily columns start in Column C. What do I change to make it start with the 3rd column now, instead of 2nd? I've figured out that "For i=2" goes to "For i=3". What else?


      Dim i As Long
        Application.ScreenUpdating = False
        With Activesheet 'change to need
            For i = 2 To .UsedRange.Columns.Count
                .Columns(i).Hidden = False
                If .Cells(1, i) < Date Or .Cells(1, i) > Date + 9 Then .Columns(i).Hidden = True
            Next
        End With
        Application.ScreenUpdating = True
    Last edited by jeffreybrown; 10-11-2016 at 06:42 PM. Reason: Please use code tags

  24. #24
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

    Short answer. Nothing else.

  25. #25
    Registered User
    Join Date
    10-05-2016
    Location
    GA
    MS-Off Ver
    2013
    Posts
    12

    Re: Hide Columns Older than Today

    So it does still hide the dated columns that I don't need, but now I need column A and (new column) B to remain unhidden by default.

    We added a 2nd column of static data in column B. See attached.

    with column B.PNG

  26. #26
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,330

    Re: Hide Columns Older than Today

      Dim i As Long
        Application.ScreenUpdating = False
        With ActiveSheet 'change to need
            For i = 3 To .UsedRange.Columns.Count
                .Columns(i).Hidden = False
                If .Cells(1, i) < Date Or .Cells(1, i) > Date + 9 Then .Columns(i).Hidden = True
            Next
        End With
        Application.ScreenUpdating = True
    Like you yourself said, change 2 to 3. Don't forget to remove formula in A1 or column will also be hidden.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If date older than today color cell RED
    By chococharlie in forum Excel General
    Replies: 5
    Last Post: 06-02-2014, 05:54 PM
  2. Auto hide columns when date is before today
    By Emrytate in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2013, 06:07 AM
  3. sum only if older than today()
    By macleod74 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-12-2013, 08:29 AM
  4. Replies: 4
    Last Post: 03-12-2012, 03:44 AM
  5. Macro to hide the columns that contains date < today
    By Darold in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-30-2010, 10:35 AM
  6. Conditional Formatting Today and Older
    By ConsbruckR in forum Excel General
    Replies: 7
    Last Post: 03-18-2009, 04:31 PM
  7. How do I add a range by date over 90 days older than today
    By John DeLosa in forum Excel General
    Replies: 4
    Last Post: 02-16-2006, 05:35 PM
  8. conditional formatting ... shade cell ... older than today
    By Mirsten Choiple in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-25-2005, 10:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1