Excel Filtering for a Summary Tab

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
880
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a 5k+ row sheet that contains Invoices, job Ids, Merchants, with Due Dates and Shipment dates among a lot of other unique data. I need a way to quickly and easily identify based off of year when something was shipped. I want to maintain one sheet lets call it data. Then be able to look at another sheet say (2024) or (2025) to reference right there automatically what has been shipped for that year. The sheet 2024 will have all the columns in the data tab that I populated. So essentially a summary filtered by Ship date of 2024. Would anyone be able to help? The user isn't very computer inclined so having them filter or advance filtering themselves is a no can do. I know Excel has FILTER and UNIQUE filter formulas that I think may work but I had issues getting them to work so looking for some help. Thank you in adcance!

Job #Shipment DateDue DateABCDXXXX
11/1/202312/2/2022AA121231234XXXX
21/1/202212/2/2021BB665577XXXX
31/1/202412/2/2023CC998824XXXX
410/1/20249/1/2024DD856874314XXXX
510/5/20209/5/2020EE101202303XXXX
69/9/20208/10/2020FF444555666XXXX
79/1/20248/2/2024GG888999110XXXX
811/1/202410/2/2024HH5687458742365XXXX
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
@Fuji so your line of code is unhiding the data set then not rehiding after. Is there a modification that can be done so it doesn’t do that OR it identifies what’s hidden then hides it when done. I did create a new post for it.

 
Upvote 0
This pastes the data into the "Summary" sheet.
VBA Code:
Sub FilterByYear()
    Dim filterYear As Long
    Dim ws As Worksheet, summarySheet As Worksheet
    Dim lastRow As Long
    Dim rng As Range
 
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set summarySheet = ThisWorkbook.Worksheets("Summary")

    filterYear = ws.Range("M2").Value
    lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
 
    ' Apply filter based on year
    ws.Range("A3:K" & lastRow).AutoFilter Field:=2, _
        Criteria1:=">=" & DateSerial(filterYear, 1, 1), _
        Operator:=xlAnd, _
        Criteria2:="<=" & DateSerial(filterYear, 12, 31)
 
    ' Check if there are visible rows
    On Error Resume Next
    Set rng = ws.Range("A4:K" & lastRow).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
 
    If Not rng Is Nothing Then
        ' Clear existing data on the Summary sheet
        summarySheet.Cells.Clear
     
        ' Copy filtered cells to the Summary sheet
        ws.Rows(3).Copy Destination:=summarySheet.Range("A1") ' Copy header
        rng.Copy Destination:=summarySheet.Range("A2")        ' Copy filtered data
    Else
        MsgBox "No data found for the specified year.", vbExclamation
    End If

    ws.AutoFilterMode = False
End Sub
@Cubist Is there a way for your code to not unhide the sheet1 rows? Or for it to rehide what was hidden before running the line of code?
 
Upvote 0
It's untested.
VBA Code:
Sub FilterByYear()
    Dim filterYear As Long
    Dim ws As Worksheet, summarySheet As Worksheet
    Dim lastRow As Long
    Dim rng As Range
    Dim hiddenRange As Range
    Dim cell As Range

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set summarySheet = ThisWorkbook.Worksheets("Summary")

    filterYear = ws.Range("M2").Value
    lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row

    ' Record the range of hidden rows
    For Each cell In ws.Rows("4:" & lastRow).Columns(1).Cells
        If cell.EntireRow.Hidden Then
            If hiddenRange Is Nothing Then
                Set hiddenRange = cell.EntireRow
            Else
                Set hiddenRange = Union(hiddenRange, cell.EntireRow)
            End If
        End If
    Next cell

    ' Unhide all rows temporarily
    ws.Rows("4:" & lastRow).EntireRow.Hidden = False

    ' Apply filter based on year
    ws.Range("A3:K" & lastRow).AutoFilter Field:=2, _
        Criteria1:=">=" & DateSerial(filterYear, 1, 1), _
        Operator:=xlAnd, _
        Criteria2:="<=" & DateSerial(filterYear, 12, 31)

    ' Check if there are visible rows
    On Error Resume Next
    Set rng = ws.Range("A4:K" & lastRow).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Not rng Is Nothing Then
        ' Clear existing data on the Summary sheet
        summarySheet.Cells.Clear

        ' Copy filtered cells to the Summary sheet
        ws.Rows(3).Copy Destination:=summarySheet.Range("A1") ' Copy header
        rng.Copy Destination:=summarySheet.Range("A2")        ' Copy filtered data
    Else
        MsgBox "No data found for the specified year.", vbExclamation
    End If

    ws.AutoFilterMode = False

    ' Re-hide rows that were originally hidden
    If Not hiddenRange Is Nothing Then
        hiddenRange.EntireRow.Hidden = True
    End If
End Sub
 
Upvote 0
So as a test I hid rows 2-20. But now only 4-20 is hidden after running. Also, I am not sure if its a big change. But could a dialog box be used for the criteria asking the user? instead of inputting in the cell of M2. If that warrants a new post I can obviously do that after we situate the hidden rows. Thanks @Cubist
 
Upvote 0
Actually I think you coded fine. Forgot my sample test I provided you guys started on row 4. but my actual data is on row 2. I just modified the Rows("4:" to Rows("2:" and seems to work.
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top