+ Reply to Thread
Results 1 to 11 of 11

Hide Columns automatically based on date

  1. #1
    Registered User
    Join Date
    11-04-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Hide Columns automatically based on date

    Dear all,

    I have a problem where I want to hide certain whole columns automatically where a date in the cells between B4:I4 is greater than the 'current date' in cell A3 [i.e. cell A3 has the value of "=NOW()"] and to show those same whole columns automatically where a date in the cells between B4:I4 is less than the current date.

    That is, for example:

    A3 =
    2 December 2011 at 4:45 PM

    B4= C4= D4= E4= F4= G4= H4= I4=
    1-Apr-11 1-May-11 1-Jul-11 1-Sep-11 1-Oct-11 1-Dec-11 1-Feb-12 1-Apr-12

    So in the the above example, whole columns H:I should hide as the value in the cells in each of H4:I4 is greater than the current date in A3 whereas columns B:G should show as the value in the cells in each of B4:G4 is less than the current date in A3.

    Does anyone know how this can be achieved (I assume a macro/VB script is required)? And how it can be achieved automatically (i.e. much the same as formulas update automatically)?

    Thanks in advance!

    Best regards,
    Chris

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Hide Columns automatically based on date

    Use this code. Right click on the sheet you are using and click on View Code. Copy this code in there. It will work each time the sheet is activated.
    Please Login or Register  to view this content.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Hide Columns automatically based on date

    Avoid loops whenever possible

    Place this code in a Standard Module

    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste
    Please Login or Register  to view this content.
    Trigger it with the WorkSheet Change event

    Worksheet event code is stored on a worksheet module. To add it to your worksheet, do the following:

    Copy the code
    Select the worksheet in which you the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Hide Columns automatically based on date

    I've amended the code to allow for the dates not starting with January.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-04-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hide Columns automatically based on date

    Thank you both for your help.

    I tried both.

    For Ardette's solution, it hides dates after the date in A3, but then when the date in A3 changes to a later it does not re-show the columns with dates before the date in A3.

    For Roy's solution, it does not appear to update the sheet automatically. It does work when I run the macro manually. However, it hides some dates but does not hide columsn for all dates after the date in A3?

    Not sure how I can resolve this, but I can try experimenting with both solutions to see if it can be resolved?

  6. #6
    Registered User
    Join Date
    11-04-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hide Columns automatically based on date

    Perhaps if I give an example spreadsheet this will help and the formula can be put into this spreadsheet?

    Having trouble uploading though?!

    Thanks again

  7. #7
    Registered User
    Join Date
    11-04-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hide Columns automatically based on date

    I think it may have something to do with this formula in the viewcode part:

    Code:

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$B$3" Then Exit Sub
    HideCols
    End Sub

    I've changed to A3 to match my spreadsheet, but it doesn't seem to update automatically still.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Hide Columns automatically based on date

    My final example hides the months after the date in the specified cell

  9. #9
    Registered User
    Join Date
    11-04-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hide Columns automatically based on date

    Thanks Roy - I still cannot get it to work. I have attached a sample file in which I have deleted all personal information but where the information is the same as my actual document. I want to be able to change the date in A3 and the columns show/hide depending on whether the date is before/after the date in A3 respectively.

    Any help you can give me is greatly appreciated as for an important work project...! Thanks for all your help so far

    Chris
    Attached Files Attached Files

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Hide Columns automatically based on date

    Your attached file doesn't have consecutive months, there's months missing.

    I'm not really sure what you are doing
    Last edited by royUK; 12-06-2011 at 02:55 PM.

  11. #11
    Registered User
    Join Date
    11-04-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Hide Columns automatically based on date

    Hi Roy,

    As per my original email where I set out the first several dates, the months are not all months as, for what I am working on, there is only data for every 2-3 months.

    Do you know how to write the formula such that it hides/shows the columns relative to A3? Your spreadsheet that you sent seems to work well so if the same thing can be achieved for this spreadsheet given your excellent VBA knowledge, that would be great

    So, just to clarify, it would be to hide all columns where the dates are after the dates in A3, no matter what I enter into A3, and then to show/hide columns depending on what is entered into A3. In the spreadsheet, I've put a little explanation if that assists you?

    Thanks again!

    Chris
    Last edited by chrisjc19; 12-06-2011 at 03:34 PM. Reason: Amend

+ 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