Pivot Header Issue

VTHokie11

New Member
Joined
Oct 26, 2011
Messages
19
I have a bit of code which will generate a pivot table on a new sheet placing the data as both the row field and data field (outputting a count on the # of occurrences of each description/word. The problem is that unless the data happens to select the header row the correct header is not grabbed and the count of the very first description (forgot to mention each cell is a qualitative description) is one less than it technically should be.

What I need the code to do instead is if the selected range does not include the top row (where the header lies) use the header from the first row, which would hopefully fix both issues. Hope I explained the scenario well enough, happy holidays!

Code:
Sub DoAll()
    
    Dim CloudData As Range
    
    On Error Resume Next
    
    'Asks user to specify which column of data they wish to summarize
    Set CloudData = Application.InputBox("Please select a range with the incident information you wish to summarize.", _
                                              "Specify Incident Information", Selection.Address, , , , , 8)

'Sub MakeTable()
Dim Pt As PivotTable
Dim strField As String
    
    'Pass heading to a String variable
    '*Need to make edit here most likely with some sort of if to use the first row heading
    'if selected data does not include this. i.e. if selected range starts with second row
    'or after.*
    
    'strField = Selection.Cells(1, 1).Text
    strField = CloudData.Cells(1, 1).Text
    
    'Name the list range not using the xlDown because there exists
    'the possibility of blanks in the column data.
    
    'Range(Selection, Selection.End(xlDown)).Name = "Items"
    CloudData.Name = "Items"
       
    'Create the Pivot Table based off our named list range.
    'TableDestination:="" will force it onto a new sheet
    
     ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
         SourceData:="=Items").CreatePivotTable TableDestination:="", _
             TableName:="ItemList"
                
    'Set a Pivot Table variable to our new Pivot Table
    Set Pt = ActiveSheet.PivotTables("ItemList")
    
    'Place the Pivot Table to start from A1 on the new sheet
    ActiveSheet.PivotTableWizard TableDestination:=Cells(1, 1)
        
    'Move the list heading to the Row Field
    Pt.AddFields RowFields:=strField
    
    'Move the list heading to the Data Field
    Pt.PivotFields(strField).Orientation = xlDataField

ActiveSheet.Name = "FreqTable"

'Sorts frequency table descending. *has stopped working since I started tweaking this code I found
'    With Range("A1", Cells(Rows.Count, 2).End(xlUp)).Resize(, 2)
'        .Sort .Cells(1, 2), xlDescending
'    End With

Call CreateCloud
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,214,845
Messages
6,121,902
Members
449,053
Latest member
Guy Boot

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