+ Reply to Thread
Results 1 to 12 of 12

Macro Button to hide rows based date in column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-10-2012
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    456

    Macro Button to hide rows based date in column

    Hello,

    I am looking to create a macro button, that when clicked will auto hide rows that have a date older than 7 days in column K. (Sample attached)

    I want to create a button, as I only want it to run when the button is clicked.

    I appreciate any help with this.

    Thank you,
    Nick
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    01-11-2019
    Location
    India
    MS-Off Ver
    2010
    Posts
    69

    Re: Macro Button to hide rows based date in column

    Check my attachment. Hope it is what is required by you.
    Attached Files Attached Files

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro Button to hide rows based date in column

    Attach this to a command button

    Option Explicit
    
    Sub HideRows()
    Application.ScreenUpdating = False
    Dim lr As Long, i As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
    If Range("K" & i) < Date - 7 Then
    Range("A" & i).EntireRow.Hidden = True
    End If
    Next i
    Application.ScreenUpdating = True
    
    End Sub
    You may want to look at this tutorial also

    https://www.automateexcel.com/vba/un...-rows-columns/
    Last edited by alansidman; 12-23-2023 at 12:30 PM.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,913

    Re: Macro Button to hide rows based date in column

    Here's another version to do the same.
    Sub HideOlderThan7Days()
        Dim rw As Range
        
        With ThisWorkbook.Sheets("Sheet1").ListObjects("Table1")
            For Each rw In .DataBodyRange.Rows
                If rw.Cells(1, "K").Value <= Date - 7 Then rw.Hidden = True
            Next
        End With
    End Sub
    BSB

  5. #5
    Forum Contributor
    Join Date
    07-10-2012
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    456

    Re: Macro Button to hide rows based date in column

    alansidman, I tested yours, but it hides everything, including the header and cells with no values. I forgot to have that option in my test file, there will be empty cells until a date is put in.

  6. #6
    Forum Contributor
    Join Date
    07-10-2012
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    456

    Re: Macro Button to hide rows based date in column

    BadlySpelledBuoy, I tested yours, and got a Run-Time error '9': Subscript out of range

  7. #7
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,913

    Re: Macro Button to hide rows based date in column

    It works for me. See attached.

    I've updated the code to the below which accounts for cells without dates.
    Sub HideOlderThan7Days()
        Dim rw As Range
        
        With ThisWorkbook.Sheets("Sheet1").ListObjects("Table1")
            For Each rw In .DataBodyRange.Rows
                If rw.Cells(1, "K") <= Date - 7 And rw.Cells(1, "K") <> "" Then rw.Hidden = True
            Next
        End With
    End Sub
    If you still get an error with this, then attach a more accurate sample file so we can investigate the reason properly.

    BSB
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    07-10-2012
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    456

    Re: Macro Button to hide rows based date in column

    BadlySpelledBuoy, Thanks, it was my error, when I changed the worksheet name, I mistyped. It takes a few seconds to complete, but does exactly what I want. Thank you!

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro Button to hide rows based date in column

    Worked for me in your test file. You need to supply a file that is representative of your actual data if you expect results to work.

  10. #10
    Forum Contributor
    Join Date
    07-10-2012
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    456

    Re: Macro Button to hide rows based date in column

    Hi Alan, attached is an updated file, it was my error in the original post, sorry about that. In the updated file, it will hide rows that have no date in the table and also it hides the row 1.
    Attached Files Attached Files

  11. #11
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro Button to hide rows based date in column

    Try this:

    Option Explicit
    
    Sub HideRows()
        Application.ScreenUpdating = False
        Dim lr As Long, i As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        For i = lr To 4 Step -1
    
            If Not Range("K" & i) = "" And Range("K" & i) < Date - 7 Then
                Range("A" & i).EntireRow.Hidden = True
            End If
        Next i
        Application.ScreenUpdating = True
    
    End Sub

  12. #12
    Forum Contributor
    Join Date
    07-10-2012
    Location
    United States
    MS-Off Ver
    Office 365
    Posts
    456

    Re: Macro Button to hide rows based date in column

    Awesome Alansidman, it runs quickly also.

+ 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. Macro VBA Toggle button to Hide/Unhide multiple rows based on a value
    By biguzivert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2022, 06:14 PM
  2. [SOLVED] Macro to unhide & hide rows based on column value
    By qvfr034 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-15-2020, 03:00 PM
  3. Creating a Command Button/Macro to hide and display rows in a Column based on value
    By garyclark21 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2015, 09:06 PM
  4. [SOLVED] Command Button to Hide rows based on a date range in Column A
    By mhewitson15 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-24-2014, 06:21 PM
  5. Macro to hide rows based on today's date and auto run it
    By evt123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-15-2014, 01:14 PM
  6. [SOLVED] VBA macro that hide and unhide column to respective rows based on first column value
    By janine6192 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-14-2013, 06:21 PM
  7. Macro to hide rows based on defined range in a column
    By derryt in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-27-2012, 12:20 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