cycle through pivot fields for all items columns that start with a changing number

Sitrean

New Member
Joined
Oct 25, 2011
Messages
17
ok, I know someone always says this was answered on another thread, but I have looked and looked for weeks and tried so many combinations my eyes are bleeding. I'm not very good at this, and my efforts to adapt other code has not been fruitful.

I have 8 identical pieces of equipment with 38 sensors each. I want to be able to cycle through each available bit of data and select each column that has a "1" or "2" etc for the first character. (someday it would also be nice to search for temp vs pressure, but baby steps, yeah?) The message i get is below, and I highlighted the buggy code in red/bold/underline
1688577768461.png


Sub add_column_field()
'add the column field to the pivot table.
'the field is determined by the button text that calls the macro. it can be 1 - 8 based on our equipment number
'source:Use VBA Macro Buttons to Add/Remove Pivot Table Fields Instantly! (on youtube)

Dim PT As PivotTable
Dim PF As PivotField
Dim sField As String '1, 2, 3, 4, etc as text
Dim shp As Shape
Dim wb As Workbook

On Error GoTo ErrHandler

Set wb = ActiveWorkbook
Set PT = ActiveSheet.PivotTables(1)
Set shp = ActiveSheet.Shapes(Application.Caller)
sField = shp.TextFrame.Characters.Text
PT.RefreshTable

'add field that button was clicked for
For Each PF In PT.PivotFields
If InStr(1, Left(PF.Name, 1), sField) > 0 Or PF.Name = "date" Then PT.adddatafield PF, PF, xlSum Else 'PF.Orientation = xlHidden
Next


ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
Resume Next
End Sub
 

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.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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