Deselecting Pivot Table Items Error - excel

I'm trying to use the code in this link, but I keep getting the runtime error 438 "Object doesn't support this property or method" for the line
ActiveSheet.PivotItems(1).Visible = True
Does anyone know what's going on?
Full Code:
Sub DeleteAllFields()
Dim i As Long
ActiveSheet.PivotItems(1).Visible = True
For i = 2 To ActiveSheet.PivotItems.Count
ActiveSheet.PivotItems(i).Visible = False
Next
End Sub
Full disclosure - I am a novice VBA user.
Thank you!

The error relates to you using .PivotItems() with the object ActiveSheet.
The PivotItems() property is part of the PivotFields() object so you would need something like:
ActiveSheet.PivotTables("PivotTable1").PivotFields("Example Field").PivotItems(1).Visible = True

Related

Is there a way to have macro check if a specific filter option exists?

Good morning,
I am trying to create a macro that will filter data based on the month, quarter, or year-to-date. I have a column in my spreadsheet called "MONYR" which includes the month and year that data point was taken from. The problem that I am having is that I might not always have data for each month. For example, the code below keeps triggering a "Run-time error '1004': Unable to get the PivotItems property of the PivotField class" because there is no "Jan-2019" option in the MONYR field as I am missing that data. Is there a way to make the macro check if the "Jan-2019" option exists, and if it doesn't, to move on to the next line without throwing an error?
With ActiveSheet.PivotTables("PivotTable1").PivotFields("MONYR")
.PivotItems("Jan-2019").Visible = True
.PivotItems("Feb-2019").Visible = True
.PivotItems("Mar-2019").Visible = True
.PivotItems("Apr-2019").Visible = False
.PivotItems("May-2019").Visible = False
.PivotItems("Jun-2019").Visible = False
.PivotItems("Jul-2019").Visible = False
.PivotItems("Aug-2019").Visible = False
.PivotItems("Sep-2019").Visible = False
.PivotItems("Oct-2019").Visible = False
.PivotItems("Nov-2019").Visible = False
.PivotItems("Dec-2019").Visible = False
End With
Thanks in advance!
One of the rare cases where it is useful to use On Error Resume Next - but don't forget to put the statement On Error Goto 0.
On Error Resume Next tells VBA to continue code execution even if an error occurs. This is bad in most circumstances as it hides errors (and therefore problems with your code). But if you know exactly that a specific line can cause an error and it is okay to continue, this statement can help you out. But once these statements are done, you should change it back to the default behavior.
On Error Resume Next ' Skip to next line if pivot entry doesn't exist.
.PivotItems("Jan-2019").Visible = True
.PivotItems("Feb-2019").Visible = True
.PivotItems("Mar-2019").Visible = True
(...)
On Error Goto 0

VBA clear ActiveX combobox list

How to clear Excel ActiveX ComboBox list with VBA. I expected this code would work:
Sheet1.OLEObjects(1).Clear
but it raises
runtime error object doesn't support this property or method.
I am puzzled because this works:
Sheet1.OLEObjects(1).Name
returning the name TempCombo.
I still fail when I try this:
Sheet1.TempCombo.Clear
It returns error:
Runtime error Unspecified error.
What else should I check?
If you want to clear the ActiveX ComboBox list, you may try something like this...
Sheet1.OLEObjects(1).ListFillRange = ""
Or more precisely this...
If TypeName(Sheet1.OLEObjects(1).Object) = "ComboBox" Then
Sheet1.OLEObjects(1).ListFillRange = ""
End If
If the combobox has a name, you can just refer to the name. Like
With mySheet
.cbMyComboBox.ListFillRange = vbNullString
End with

DialogSheets("Name").Labels.Visible = True not working on all DialogSheets in Excel VBA

I've been given an ancient but, for the most part, working Excel solution.
It uses a lot of DialogSheets (an Excel 5/95 technique before UserForms).
Now I'm trying to figure out what the author tried to do, the code is pretty horrible. One problem I stumbled across is this:
Some sheets work fine with code like:
DialogSheets("Name1").Show
DialogSheets("Name1").Labels.Visible = True
DialogSheets("Name1").EditBoxes.Visible = True
DialogSheets("Name1").CheckBoxes.Visible = True
DialogSheets("Name1").DropDowns.Visible = True
On others, I get a:
runtime error 1004 "Unable to set the Visible property of the Labels class"
I cannot quite work out the difference, and the documentation on Dialogsheets is pretty sparse. If anyone had any idea I'd appreciate a suggestion.
Update
The answers suggest that some DialogSheets may not have Labels, thus throwing the 1004. This is not the case. This happens in the ImmediateWindow:
?DialogSheets("Name1").Labels.Count
205
?DialogSheets("Name2").Labels.Count
7
DialogSheets("Name1").Labels.Visible = True ' Throws 1004
DialogSheets("Name2").Labels.Visible = True ' executes fine
?DialogSheets("Name2").Labels.Visible = True ' returns true
True
The code above sets the visibility of the Labels, DropDowns, EditBoxes and CheckBoxes to True.
It throws error 1004, because on some DialogSheets the Labels, DropDowns, EditBoxes or CheckBoxes are missing. And it works on those, where all 4 are present.
To see it working, close all Excel applications. Then open a brand new one and run this code:
Sub TestMe()
Dim someDialogSheet As DialogSheet
Set someDialogSheet = ThisWorkbook.Sheets.Add(Type:=xlDialogSheet)
With someDialogSheet
.Name = "SoReadyToHelp"
.Labels.Add(5, 5, 5, 5).Select
.DropDowns.Add(10, 10, 10, 10).Select
.CheckBoxes.Add(15, 15, 15, 15).Select
Debug.Print .Labels.Visible
Debug.Print .CheckBoxes.Visible
Debug.Print .DropDowns.Visible
.Show
End With
End Sub
Or there is On Error Resume Next somewhere on the code where it works.
The code will fail if there are no labels to make visible.
Add a check like this, to make sure there are any before making them visible:
If DialogSheets("Name1").Labels.Count > 0 Then
DialogSheets("Name1").Labels.Visible = True
End If
Edit after comment
Then I suggest you try something like this:
For Each lbl In DialogSheets("Name1").Labels
lbl.Visible = True
Next

Report Filter on a single item in pivot table

I have the following code to ensure that only a single value is selected on the relevant pivot field - however I keep getting the error "unable to set the currentpage property of the pivot field" - I don't understand why as I used a the same code for a different filter.
Sub filters()
Dim PRC As PivotField
Sheets("workings-calc").Select
Set PRC = ActiveSheet.PivotTables("PivotTable3").PivotFields("PR_CD")
PRC.ClearAllFilters
PRC.CurrentPage = "PR-500001"
End Sub
Appreciate any help on this. Have spent far too long trying to get this to work without success :/
The following code works and gets me the desired output.
for each itm1 in prc.pivotitems
if item 1.name = "PR-500001" then
else
itm1.visible = false
next
the above code leaves only the filter specified selected on the pivot field.
Thanks for all the input.

Excel VBA Missing Reference - PI Osisoft

I have an VBA code where I use many objects from PISDK, which I have to add as reference to my project.
I must explicitly declare the variables otherwise the code won't work. I don't know why. Excel throws an error ("types doesn't match") if I declare, for example, pt as object instead of PIPoint.
Here is part of my code:
Dim srv As Server
Dim pt As PIPoint
Dim pv As PIValue
Dim dt As New PITimeFormat
The problem is: when user doesn't have this reference installed, Excel gives me an compilation error, so it's impossible to catch and handle this error. Since this code runs on a user-defined function, as soon as the user opens the workbook, he gets stuck with compiling errors.
I must be able to catch this error.
I can't find documentations to fully implement late binding on this code. I don't know if it's really possible to do it. I know it could solve my problem.
Also, I know I could check if the reference is installed, thru:
thisworkbook.vbproject.references
But if the user doesn't allow access to the vbaProject object under Excel options, I am not able to do this.
Any idea?
I managed to solve my problem declaring everything as object and then using createobject afterwards.
The main problem doing this was using functions, like this one:
I have the function "arcValue". It takes three arguments:
arcValue(TimeStamp as PITimeFormat, Mode as RetrievelTypeConstants, Optional asynchStatus as PIAyncnStatus)
The way I use it doing early binding is:
dim pt as PIPoint
dim pv as PIValue
set pv = pt.data.arcValue("01/09/2014 17:00:00", rtInterpolated)
This works. But when I do:
Dim myPISDK As Object
Dim srv As Object
Dim pt As Object
Dim pd as Object
Dim pv as Object
Set myPISDK = CreateObject("PISDK.PISDK")
Set pv = CreateObject("PISDK.PIValue")
Set srv = myPISDK.Servers.defaultserver
Set pd = pt.DATA
Set pt = srv.PIPoints("piTAG")
Set pv = pd.ArcValue("01/09/2014 17:00:00", rtInterpolated)
It doesn't work. But why?
There were two problems:
First: When I use late binding (createobject) I don't have access to "rtInterpolated" constant, so I have to use its equivalent number.
Set pv = pd.ArcValue("01/09/2014 17:00:00", 3)
But this still doesn't work. So I had to do this to make it work:
Set pv = pd.ArcValue("01/09/2014 17:00:00", 3, Nothing)
And then everything started working. I don't know why, but VBA makes me write something do all parameters, even if they are optional.
This way, I am able to detect erros in runtime, so I used this code:
If myPISDK Is Nothing Then
piVerified = "Erro PI"
Exit Function
End If
Also, I had to remove all references (they are not used anymore, anyway) because it was causing malfunction on other parts of my code not related to this one when the references were missing.
You can use something like that:
Sub NotUsed()
Dim pt As PIPoint
End Sub
Function ReferenceCheck() As Boolean
On Error GoTo NoRef
pt = Acrobat.AV_DOC_VIEW ' PIPoint
ReferenceCheck = True
Exit Function
NoRef:
ReferenceCheck = False
End Function
Sub Test()
If ReferenceCheck Then NotUsed
End Sub
The function refer to a proprieties of the object. If the reference it's ok return true otherwise false.
In the phase of init you can check like that.
The sub NotUsed, don't create Error because not called...
In my sample I use the ADOBE Object ...

Resources