Filter, Count Filtered results, Copy the Filtered result to Next sheet using VBA.

99Ahmad99

New Member
Joined
Dec 19, 2021
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a workbook consisting of two sheets. The sheets have headers I don't need to touch.
First sheets, information started at row 12 to row 2450. what I need is a VBA code that first filters the data located in columns (I) based on cell value located in columns(S), (AD) and (AO). in those columns, there is a cell value which is a letter(like B, C, ), so each row that belongs to columns (I) has a letter in columns(S) (AD) and AO contains letters (B,C,D). count them and paste it into a table located into next sheet named sheet2 into cell G45 yo G73.

this is what I tried to do with a help of friend
Code.jpg
:
Sub PivotTable_Sum()

' Get the worksheets
Dim shRead As Worksheet, shWrite As Worksheet
Set shRead = ThisWorkbook.Worksheets("Transactions")
Set shWrite = ThisWorkbook.Worksheets("Report")

' Get the range
Dim rg As Range
Set rg = shRead.Range("A1").CurrentRegion

' Clear any existing pivot tables
Dim piv As PivotTable
For Each piv In shWrite.PivotTables
piv.TableRange2.Clear
Next piv

' Clear the data in output worksheet
shWrite.Cells.ClearContents

' Create the cache
Dim ptCache As PivotCache
Set ptCache = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase _
, SourceData:=rg _
, Version:=xlPivotTableVersion15)

' Create the table
Dim ptTable As PivotTable
Set ptTable = shWrite.PivotTables.Add( _
PivotCache:=ptCache _
, TableDestination:=shWrite.Range("A1"))

' Set the name and style
ptTable.name = "pvItems"
ptTable.TableStyle2 = "PivotStyleDark14"

' Set the fields
Dim ptField As PivotField
With ptTable

' Set the rows fields
.PivotFields("Item").Orientation = xlRowField

' Set the data(value) fields
.PivotFields("Volume").Orientation = xlDataField
.PivotFields("Sales").Orientation = xlDataField

End With

End Sub
Sub PivotTable_Sum()


Dim shRead As Worksheet, shWrite As Worksheet
Set shRead = ThisWorkbook.Worksheets("Transactions")
Set shWrite = ThisWorkbook.Worksheets("Report")

' Dim rg As Range
Set rg = shRead.Range("xxx").CurrentRegion

' Clear any existing pivot tables
Dim piv As PivotTable
For Each piv In shWrite.PivotTables
piv.TableRange2.Clear
Next piv

' Clear the data in output worksheet
shWrite.Cells.ClearContents


Dim ptCache As PivotCache
Set ptCache = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase _
, SourceData:=rg _
, Version:=xlPivotTableVersion15)


Dim ptTable As PivotTable
Set ptTable = shWrite.PivotTables.Add( _
PivotCache:=ptCache _
, TableDestination:=shWrite.Range("A1"))


ptTable.name = "pvItems"
ptTable.TableStyle2 = "PivotStyleDark14"


Dim ptField As PivotField
With ptTable

' Set the rows fields
.PivotFields("Item").Orientation = xlRowField

' Set the data(value) fields
.PivotFields("Volume").Orientation = xlDataField
.PivotFields("Sales").Orientation = xlDataField

End With

End Sub
 

Attachments

  • Code 2.jpg
    Code 2.jpg
    46.5 KB · Views: 7

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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