Excel VBA - PivotItems returns inexesting value - vba

In an Excel Workbook, I have "static" pivot table on a sheet, that is based on data from another sheet.
I'm refreshing the data on my data sheet (thank you captain Obvious !), then I want to show ALL the items, exept the blank one, so I'm running throw all the PivotItems to set them to visible, and, at the end, unselecting the blank one :
i = 1
ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").ClearAllFilters
ThisWorkbook.Sheets("TCD").PivotTables(i).PivotCache.MissingItemsLimit = xlMissingItemsNone
For Each PvI In ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems
PvI.Visible = True
Next
ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems("(blank)").Visible = False
At the last occurs of my loop, on the 4th PivotItems, I have an error of execution '1004' (I'll translate it from french, it may me some errors, sorry) "Impossible to define the property Visible of the class PivoItem", so I checked a few things :
?ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems.count
4
for x = 1 to 4 :
?ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems(x)
(blank)
SFT
ACQ
TEP
It look like I have 4 items in my Pivot Table, but
And also, when I check my datas, I only have 2 different stats :
So where does this 4th PivotItems' element is coming from, and how can I get ride of it ?
Thank you.

I had a surprising issue like that, you need to check in the Pivot Table options :
Right click on the pivot table,
Select Pivot Table options
Go in Data tab
Find Retain items deleted from the data source
Choose None for Number of items to retain per field!
Then refresh! All should be OK now! ;)
(that thing drove me mad for hours!^^)

Related

Using VBA User Form to filter a pivot table (esp. in ways the Report Filter and Slicers can't)

I have a pivot table, created manually. There are ways users need to be able to filter it which can't be done through Report Filters or Slicers, so I thought VBA would be the way to go. I've built a skeletal user form, and am now at the tricky stage of writing the code behind it.
I'm trying to use the PivotFilters function, but this isn't working. I will admit that I am an utter novice at VBA, so it may be a simple error I've made, or perhaps PivotFilters aren't the right tool for the job? Here is an example:
'ok button - this is the big one, telling the button what to do with the selected options!
Private Sub CommandButton1_Click()
'targeting our familiar pivot table
Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables("PivotTable1")
'Whether to filter out low sample size brands
Dim pvtFieldSasz As PivotField
Set pvtFieldSasz = pvt.PivotFields("SampleSize")
If OptionButton1 = True Then
pvt.pvtFieldSasz.PivotFilters.ClearAllFilters
pvt.pvtFieldSasz.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Reliable Sample"
End If
End
The above example is something that can be done with Report Filters or Slicers, but I wanted to put all the relevant options in the user form, to make it a one-stop-shop for the user.
The error message I get is "Run-time error '438': Object doesn't support the property or method".
I haven't moved on to the trickier ones yet, as I want to master the basics. As an example of what I'd like to achieve in a different filter:
The filter would refer to one of three separate columns, depending on the option selected by the user. The user would have three option buttons, of which they can only select one at a time. If they select OptionButton1, I would like the pivot table to be filtered on field X = 1. If they select OptionButton2, it should filter on Field Y = 1. OptionButton3 corresponds to Field Z = 1. None of these fields X, Y or Z will actually be displayed in the pivot table as a row or column, but they are part of the source data and are included in the selection behind the pivot table.
Thank you for your patience helping this stumbling novice - very grateful for any tips!
The problem is in these 2 lines:
pvt.pvtFieldSasz.PivotFilters.ClearAllFilters
pvt.pvtFieldSasz.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Reliable Sample"
First, pvtFieldSasz already represents the pivot field. Trying to specify it with pvt.pvtFieldSasz won't work because pvtFieldSasz is not a property or method of the pivottable, pvt. Instead, you should just use pvtFieldSasz. This will also be a problem in the immediately following statement.
Second, the ClearAllFilters method does not apply to the PivotFilters Property of the pivotField. Instead, it applies to the pivotField itself.
Combining these two issues for working code:
pvtFieldSasz.ClearAllFilters
pvtFieldSasz.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Reliable Sample"
As a sidenote, I'd also specify the sheet instead of using ActiveSheet as ActiveSheet can have issue, so
Set pvt = Sheets("Sheet1").PivotTables("PivotTable1")

Copy row if value of one cell is "x"; and, auto-hide blanks

I'm trying to copy a row, columns A-J, representing a person and associated info, when column J is filled. I'd fill it with either w, x, y or z based on the reason people are away, and I'd like the row (A-J) copied to another sheet as soon as J is filled.
Here's a screenshot with both Excel documents. Please ignore the "Reason" column in the Pers sheet - I know it could be used, but my production one doesn't have this column, and will not. I just used it to illustrate for you guys.
That other sheet (Main) has headers, as you can see, for each reason why people would be away. This one would permanently be displayed on a big-screen TV. So, what I'm trying to do is :
Copy the rows to Main as soon as the "Departed?" row is filled in Pers;
Make sure those rows go into the proper category, in the first blank row;
Ensure each category remains collapsible.
You can see I've tried using the If function (syntax in screenshot). That worked OK, but :
Left blanks;
Biggest one : only worked row-for-row! (Also the cause of reason #1, I suppose...) Past row 11, nothing. Not too sure what to do about that...?
Hope this is somewhat clear? =) Let me know if you have any questions - thanks for your help!

Development DropDowns List error on empty value

I have a simple dropdown list named DD8. It uses 50 rows as control, the problem is that for now only 45 rows are used. That means that in the dropdown list there are 5 empty rows. The problem is that if someone select one empty row, or don't select anything (default is empty) the fallowing code will show error :
With Worksheets(1)
NameProf = .DropDowns("DD8").List _
(.DropDowns("DD8").ListIndex)
End With
I tried things like if .DropDowns("DD8").List (.DropDowns("DD8").ListIndex) != "" but ofc, it shows error. I searched how to select only used rows with the DropDown list of the development tab but it doesn't seem to be possible.
I have to select 50 rows because new customers can be added.
Do you know how it can be achieved ?
If new customers can be added, then I imagine, and hope for you, that it goes above 50.... so it's not just an issue of having 5 blanks for now to not be an option, but also allowing customers 50-2,483 also be on there when they come along. ---- Without more details on your code, I believe this suggestion should help 'guide' you but not immediately solve your issue.
Essentially, whenever you call to have your dropdown populated, you want some code to find the last row of data in the customers column, and then assign your dropdown to populate with the starting row of your customers to the lastrow. This way no matter how many customers you have 2, 48, 189... they will show in your dropdown without that issue occurring. A simple google search will yield how to find the last row in excel.
Sorry I couldn't just bust out the code to make it work for you right this second, but I think this should be a good starting point for you.

Get the id of the selected value in a dropdown validation list on Excel in VBA

Using Excel, lets say I have a validation list made of 5 values like this one :
Patate
Tomate
Courgette
Concombre
Patate
In a cell containing a drop down list made of these 5 value, I select the fifth value : "Patate".
I want to get in VBA that this cell contains the 5th value of my validation list. Something like :
x = Cell.Validation.GetIDValueSelected
x = 5
I can't use Vertical search because I might have 2 or even more time the same value in my list (too long to explain why).
This list is also dynamics (depending of another sheets) so it doesn't always contains 5 values.
I hope I made it clear for everyone to understand my needs but I will be glad to add more information if needed.
Thank you for your time.
Sadly, once you have used DV to fill a cell with junk, there is no way to tell which piece of junk you picked:
You would have to pad each piece of junk with a different number of blanks.

How to move one column to the end in Pivot table with VBA?

I create a pirvot table, but the colunm order is not what I wanted. I want to move one column to right end. I can do it in Excel, but don't know how to do it with VBA.
In Excel, can do in this way--active the column header which you want to move, click right-hand button, choose Move, Move "--" to end.
Record Macro--when record Macro, it just shows the exact position, just like,
ActiveSheet.PivotTables("PivotTable16").PivotFields("wk").PivotItems("#VALUE!") _
.Position = 12
But I want to use the Macro in other files, maybe the end position is not 12. Then the Macro can't be used.
Hope can get help here. How to do it to move the colunm to end by VBA? Thanks.
I hope I get you right.
You can get the end position by
ActiveSheet.PivotTables("PivotTable1").PivotFields("whatever").PivotItems.count
Basically it returns the number of items for a label
Edited
So you could do like
Dim total as Integer
total = ActiveSheet.PivotTables("PivotTable1").PivotFields("whatever").PivotItems.count
ActiveSheet.PivotTables("PivotTable1").PivotFields("whatever").PivotItems("whatever").position = total
Just a random note to anyone that comes across this and runs into the same issue I did. If in your macro you're hiding fields, it's going to give you an error. Don't use this code as the last thing you do when setting up your pivot table. Use this code, let it move to the bottom position, THEN have your code to hide your fields. If you hide fields first, it'll count the hidden fields then try to move it outside your pivot table and cause an error.