Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frequency by Pivot table

Status
Not open for further replies.

feipezi

IS-IT--Management
Aug 10, 2006
316
US
Hi,
I tried to set up a frequency (count) on a field (column A for instance) using Pivot table. Here is the code.
*****************************************
Sub freqonactive(col As String)
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim tempsheet As Worksheet
Dim varname As String

Application.ScreenUpdating = False

On Error Resume Next
Application.DisplayAlerts = False
Sheets("FreqTab").Delete

LastRow = ActiveSheet.UsedRange.Rows.Count
Set PTCache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=ActiveSheet.Range(col & "1:" & col & LastRow))
varname = ActiveSheet.Range(col & "1").Value

Set tempsheet = Worksheets.Add
tempsheet.Name = "FreqTab"
Set PT = PTCache.CreatePivotTable _
(TableDestination:=tempsheet.Range("a1"), _
TableName:="one")
PT.PivotFields(varname).Orientation = xlRowField
With PT.PivotFields(varname)
.Orientation = xlDataField
.Function = xlCount
.Name = "Count"
.NumberFormat = "#,##0"
End With
With PT.PivotFields(varname)
.Orientation = xlDataField
.Name = "PCT"
.Calculation = xlPercentOfColumn
End With
With ActiveSheet.PivotTables("one").DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Sub test()
freqonactive "a"
End Sub
**************************************
Running this macro will give me 3 fields: values (column A in the example above), count, percentage.
However, if the field that I am running the macro on has blanks, the macro will ignore them; the percentage of the blanks will be 0.00%. But I need the number of blanks and their percentage in the total.
Can I make it show in the report (freqtab is the tab mentioned in the macro).
Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top