+ Reply to Thread
Results 1 to 5 of 5

hide columns based on date

Hybrid View

mattmorris hide columns based on date 07-24-2012, 09:57 AM
protonLeah Re: hide columns based on date 07-24-2012, 06:12 PM
mattmorris Re: hide columns based on date 07-25-2012, 03:16 AM
protonLeah Re: hide columns based on date 07-25-2012, 05:08 PM
mattmorris Re: hide columns based on date 07-26-2012, 05:10 AM
  1. #1
    Registered User
    Join Date
    06-28-2012
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    25

    hide columns based on date

    Hi, on row 4 i have a list of column headers starting at F4 containing the weekending dates (friday). I wish to have a macro which would hide every column apart from the current week and the previous two weeks. I've lookied online and found little to help me with this.

    ---------- Post added at 08:57 AM ---------- Previous post was at 07:52 AM ----------

    anyone got any ideas?

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,964

    Re: hide columns based on date

    Without a sample workbook I'm only guessing:
    Option Explicit
    
    Sub HideColumns()
        Dim CrntDate As Date
        Dim DayNum As Long
        Dim DateFriday As Long
        Dim x As Variant
        Dim BackTwo As Date
        Dim lastcol As Long
    
        CrntDate = Date
        DayNum = Weekday(CrntDate)
        
        x = 6 - DayNum
        CrntDate = Date + x
        
        BackTwo = CrntDate - 18
        
        lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
        
        Set x = Range("F4", Cells(4, lastcol)).Find(BackTwo)
        x = Split(x.Address, "$")
        
        Columns("F:" & x(1)).EntireColumn.Hidden = True
    End Sub
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    06-28-2012
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: hide columns based on date

    Book1.xlsm

    Heres the sheet I'm using, couldn't quite get your code to work.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,964

    Re: hide columns based on date

    This one will work if the worksheet only has columns tor Jan - Dec of one year only.
    Sub HideColumns()
        Dim TwoWeeksAgo As Long
        Dim ThisWeek    As Long
       
        ThisWeek = WorksheetFunction.WeekNum(Date)
        TwoWeeksAgo = (ThisWeek - 3)
        Columns("F:BE").EntireColumn.Hidden = True
        
        Range(Cells(1, TwoWeeksAgo + 5), Cells(1, ThisWeek + 5)).EntireColumn.Hidden = False
    End Sub

  5. #5
    Registered User
    Join Date
    06-28-2012
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: hide columns based on date

    found another way around this. thanks alot anyway!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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