winstela
New Member
- Joined
- Feb 24, 2019
- Messages
- 28
- Office Version
- 2016
- Platform
- Windows
Hi All,
Hope you can help me.
I have a table, and above the table I have subtotal formulas. I want to be able to filter by 2 columns say (Date) and (Product). then copy just the subtotals to a new sheet.
I have found code that copies the data in the table based on date ranges to a new workbook but I am struggling to add another criteria and then just to copy the row with the subtotals in
I know I can put in a pivot table but my end destination workbook is struggling with the getpivotdata formula.
Sample Data Sheet
Sample SUM Sheet. This will have the values from the Data sheet Row 1
Below is Data sheet filtered on Product A & date March
Below is the code that asks for 2 date ranges and then copies the filtered data to another workbook
Thanks
Hope you can help me.
I have a table, and above the table I have subtotal formulas. I want to be able to filter by 2 columns say (Date) and (Product). then copy just the subtotals to a new sheet.
I have found code that copies the data in the table based on date ranges to a new workbook but I am struggling to add another criteria and then just to copy the row with the subtotals in
I know I can put in a pivot table but my end destination workbook is struggling with the getpivotdata formula.
Sample Data Sheet
Sample SUM Sheet. This will have the values from the Data sheet Row 1
Below is Data sheet filtered on Product A & date March
Below is the code that asks for 2 date ranges and then copies the filtered data to another workbook
Code:
Option Explicit
'This subroutine prompts the user to select dates
Public Sub PromptUserForInputDates()
Dim strStart As String, strEnd As String, strPromptMessage As String
'Prompt the user to input the start date
strStart = InputBox("Please enter the start date")
'Validate the input string
If Not IsDate(strStart) Then
strPromptMessage = "Oops! It looks like your entry is not a valid " & _
"date. Please retry with a valid date..."
MsgBox strPromptMessage
Exit Sub
End If
'Prompt the user to input the end date
strEnd = InputBox("Please enter the end date")
'Validate the input string
If Not IsDate(strStart) Then
strPromptMessage = "Oops! It looks like your entry is not a valid " & _
"date. Please retry with a valid date..."
MsgBox strPromptMessage
Exit Sub
End If
'Call the next subroutine, which will do produce the output workbook
Call CreateSubsetWorkbook(strStart, strEnd)
End Sub
'This subroutine creates the new workbook based on input from the prompts
Public Sub CreateSubsetWorkbook(StartDate As String, EndDate As String)
Dim wbkOutput As Workbook
Dim wksOutput As Worksheet, wks As Worksheet
Dim lngLastRow As Long, lngLastCol As Long, lngDateCol As Long
Dim rngFull As Range, rngResult As Range, rngTarget As Range
'Set references up-front
lngDateCol = 2 '<~ we know dates are in column B
Set wbkOutput = Workbooks.Add
'Set wks = ThisWorkbook.Worksheets("Data")
Set wks = Workbooks("Test.xlsm").Worksheets("Data")
'Loop through each worksheet
'For Each wks In ThisWorkbook.Worksheets
With wks
'Create a new worksheet in the output workbook
Set wksOutput = wbkOutput.Sheets.Add
'wksOutput.Name = wks.Name
wksOutput.Name = "New"
'Create a destination range on the new worksheet that we
'will copy our filtered data to
Set rngTarget = wksOutput.Cells(1, 1)
'Identify the data range on this sheet for the autofilter step
'by finding the last row and the last column
lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Set rngFull = .Range(.Cells(4, 1), .Cells(lngLastRow, lngLastCol))
'Apply a filter to the full range to get only rows that
'are in between the input dates
With rngFull
.AutoFilter Field:=lngDateCol, _
Criteria1:=">=" & StartDate, _
Criteria2:="<=" & EndDate
'Copy only the visible cells and paste to the
'new worksheet in our output workbook
Set rngResult = rngFull.SpecialCells(xlCellTypeVisible)
rngResult.Copy Destination:=rngTarget
End With
'With wks
'Clear the autofilter safely
.AutoFilterMode = False
' If .FilterMode = True Then
'.ShowAllData
' If wks.AutoFilterMode Or wks.FilterMode Then
' wks.ShowAllData
Call ClearTableFilters
'Let the user know our macro has finished!
MsgBox "Data transferred!"
End With
End Sub
Thanks