Closed Thread
Results 1 to 16 of 16

VBA to hide columns based on input date range

  1. #1
    Registered User
    Join Date
    03-25-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    VBA to hide columns based on input date range

    Hi All.

    I need some VBA code to hide columns if they are outside of a specified date range.

    - the worksheet i need to run this VBA on is named 'Summary'
    - Columns A to G need to remain un-hidden at all times
    - from H17 to ZZ17 i have every month of the relevant years listed (all there chronologically)
    - D3 holds the 'Date From' variable
    - D4 holds the 'Date To' variable

    Any help would be appreciated.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: VBA to hide columns based on input date range

    This assumes that the values in row 17 are actual dates.

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    03-25-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA to hide columns based on input date range

    Hi Bernie.

    This unfortunately doesn't seem to work. When i run the macro over the sheet it hides all the columns immediately.

    Some more information:
    - the format for row 17 is custom "mmm yy" with the first cell being 1/7/2008 and the =EDATE+1 formula carried across from there (if that makes any difference?)
    - I need the columns to unhide when the range is changed (unsure if this makes a difference to your code)
    - unsure if it will matter if the date range is say 1/7/12 to 30/6/13 given the second date isn't on the first of the month (as is the case with row 17)

    Thanks.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: VBA to hide columns based on input date range

    Are the values in D3 and D4 actual dates? Can you post a file that has nothing but row 17? The code works fine in my test workbook..... The code can be run anytime D3 or D4 changes, but let's get the bugs worked out first.

  5. #5
    Registered User
    Join Date
    03-25-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA to hide columns based on input date range

    Hi Bernie,
    I've attached the example worksheet.
    Thanks.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: VBA to hide columns based on input date range

    Well, first off, your start and end dates are not in D3 and D4, but in F3 and F4. Futher, your From Date (which should be the starting date) is later than your To Date (which should be the End Date). Since your dates are inverted, the comparison was always going to hide all columns. But this will work. no matter how you arrange the dates in F3 and F4....

    Sub TestMacro()
    Dim rngC As Range
    Cells.EntireColumn.Hidden = False
    With Worksheets("Cashflow Summary")
    For Each rngC In .Range("H17:Z17")
    If rngC.Value < Application.Min(.Range("F3:F4")) Or _
    rngC.Value > Application.Max(.Range("F3:F4")) Then
    rngC.EntireColumn.Hidden = True
    End If
    Next rngC
    End With
    End Sub

  7. #7
    Registered User
    Join Date
    03-25-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA to hide columns based on input date range

    Thanks Bernie.
    Is there a way to make this code dynamic. So when the dates are changed the macro runs automatically?

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: VBA to hide columns based on input date range

    OF course - copy this, right-click the sheet tab, and select "View Code" and paste the code into the window that appears.
    Please Login or Register  to view this content.
    If you changed the macro name from "TestMacro", update that name in the code.

  9. #9
    Registered User
    Join Date
    03-25-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA to hide columns based on input date range

    Thanks Bernie.

  10. #10
    Registered User
    Join Date
    11-06-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: VBA to hide columns based on input date range

    Hi. Is there a way to adjust this so when D3 & D4 are empty, all cells are shown?

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,331

    Re: VBA to hide columns based on input date range

    Add a line like:

    If Application.CountBlank(Range("D3:D4")) = 2 Then Cells.EntireColumn.Hidden = False

  12. #12
    Registered User
    Join Date
    11-06-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    32

    Re: VBA to hide columns based on input date range

    Thank you Bernie. Worked like a charm.

  13. #13
    Registered User
    Join Date
    03-26-2012
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: VBA to hide columns based on input date range

    Thanks a lot Bernie for the awesome code, also thanks Jace15 for asking this question

  14. #14
    Registered User
    Join Date
    07-27-2013
    Location
    Ahmedabad, India
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: VBA to hide columns based on input date range

    If wants only one date instead if range, then what to do

  15. #15
    Registered User
    Join Date
    07-27-2013
    Location
    Ahmedabad, India
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: VBA to hide columns based on input date range

    In below case C4 and C5 are two date selection cell, but what if i wants single date i.e. in C4 and leave C5 blank, what modification to be done in below

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim dt As Range
    If Intersect(Target, Range("C4:C5")) Is Nothing Then Exit Sub
    Columns("E:AI").Hidden = False
    For Each dt In Range("E8:AI8")
    If dt.Value < [C4] Or dt.Value > [C5] Then Columns(dt.Column).Hidden = True
    Next dt
    End Sub

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,800

    Re: VBA to hide columns based on input date range

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to hide columns in a range based on one cells date value
    By Crawfinator1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-16-2013, 12:59 AM
  2. [SOLVED]Hide Rows and Columns Based on User Input
    By thesteve in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2013, 01:32 PM
  3. [SOLVED] Hide Columns based on date
    By Blue Fishey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-15-2012, 12:28 AM
  4. [SOLVED] hide columns based on date
    By mattmorris in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2012, 05:10 AM
  5. [SOLVED] Hide columns based on cell value - Unable to set hidden range property of Range Class
    By trillium in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-13-2012, 11:13 PM

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