Can some quicly explain the way to deselect all items in a newly created pivot table so that I can go back and select only one or two items? I tried the following:
.PivotItems("(Select All)").Visible = False
Thanks.
This is probably the closest you can get to what you want:
Dim i As Long
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next
This will make the very first option the only selected option (assuming this is within a with that points to the pivotfield). If you know what you want before hand... modify accordingly.
I've found that looping through each data item takes a lot of time, what you can do if you want to filter on a single item in a pivot without looping through all items is use the following code:
ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").ClearAllFilters
ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").PivotFilters.Add _
Type:=xlCaptionEquals, Value1:="Your string here"
this is basically a label filter but it worked for me.
Check the following out. Select data for specific field name. Please do note that you have to at least select one item by default. And also do not forget that if you want to hide items, Only contiguous items in a PivotTable Field can be hidden. Perhaps at page load, or worksheet open or any of your other sub trigger, you could select a particular items to be selected based on a specific field. Then allow your code to proceed with anything else.
Sub specificItemsField()
Dim pf As PivotField
Dim pi As PivotItem
Dim strPVField As String
strPVField = "Field Name"
Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PivotFields(strPVField)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
pf.AutoSort xlManual, pf.SourceName
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
pf.AutoSort xlAscending, pf.SourceName
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
This is how I do for custom filter selection. May be slower due to double looping.
Dim toSelect(1 To 3) As String
toSelect(1) = "item1"
toSelect(2) = "item2"
toSelect(3) = "item3"
For Each pvItem In objField.PivotItems
For Each st In toSelect
If pvItem.Value = st Then
pvItem.Visible = True
Exit For
Else
pvItem.Visible = False
End If
Next
Next
Well.
Because you have not how to hide all, because, always you need to have 1 item visible
I do this:
I start hiding the first field, and before go to the next, i show all fields i need visible, then, i go to the secont item, and hide, and again show all items i want, and so on. Then, always will be visible any field, and wont have error.
After the loop, i again try to show all fields i want.
With ActiveSheet.PivotTables("TablaD2").PivotFields("Entity")
Dim i As Long
For i = 1 To .PivotItems.Count
.PivotItems(i).Visible = False
.PivotItems("ARG").Visible = True
.PivotItems("BRL").Visible = True
.PivotItems("GCB").Visible = True
.PivotItems("MEX").Visible = True
Next
.PivotItems("ARG").Visible = True
.PivotItems("BRL").Visible = True
.PivotItems("GCB").Visible = True
.PivotItems("MEX").Visible = True
End With
Related
I need help with some rather easy macro, but I can't do that. So all I want is to switch between filters - I have two possibilities, by example two colours - black and white, and I only want to have one of them in the moment (I want to add button, so it will work as switch). Here is the situation
I've tried did it with conditional, of course it doesn't work, but hope it will helps you to understand my idea. If one filter is enable, the other one is disabled (so the objects connected with that obviously too).
Sub Makro5()
ActiveSheet.PivotTables("PivotTable3").PivotFields("colour"). _
CurrentPage = "(All)"
If ActiveSheet.PivotTables("PivotTable3").PivotFields("colour")
.PivotItems("black").Visible = False
.PivotItems("white").Visible = True
Then ActiveSheet.PivotTables("PivotTable3").PivotFields ("colour")
.PivotItems("black").Visible = True
.PivotItems("white").Visible = False
Else: ActiveSheet.PivotTables("PivotTable3").PivotFields ("colour")
.PivotItems("black").Visible = False
.PivotItems("white").Visible = True
End Sub
Something is wrong with If, because it's on red and I get "Complie error: Syntax error" info
This is what slicers are built for.
Put your cursor inside the the pivottable data area. Then goto insert > slicer > and choose colour. You will then have a slicer which will allow you to select either or. I don't know if you can disable multiselect in current excel version so user could press ctrl and select both. But this seems a simple way.
Or you could run code such as the following and then user just types in Black or White in to field and filter is applied:
Sub Test()
Dim wb As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet3")
Set pvt = ws.PivotTables("PivotTable3")
Dim pvtField As PivotField
Dim item As Long
Dim item2 As Long
Set pvtField = pvt.PivotFields("Colour")
pvtField.EnableItemSelection = False
End Sub
You need to have the Then on the same line as the If. (You can use _ to tell VBA that "code continues on next line")
You also don't have your And set up correctly, and you are making both "Black" and "White" visible before you check if they are visible?
Try this:
Sub ToggleColour()
With ActiveSheet.PivotTables("PivotTable3").PivotFields("colour")
If .PivotItems("white").Visible=True Then
.CurrentPage = "black"
Else
.CurrentPage = "white"
End If
End With
End Sub
I have a report in which I am asking the users to click buttons to reveal where they need to add their commentary. I have it working but wanted to put in an If statement in case they have already expanded the row.
I have two macros, the first relates to the button they push and sends to the main macro the name of the button and a row number which is part of the section that is either expanded or collapsed
Sub ROccupancy()
'
Dim RecName As String
RecName = "ROccupancy"
Dim RowNum As Integer
RowNum = 27
Call ToogleRec(RecName, RowNum)
End Sub
The next macro is where I am having the trouble
Sub ToogleRec(RecName, RowNum)
'
Dim Toogle As String
Dim MyObj As Object
Set MyObj = ActiveSheet.Shapes.Range(Array(RecName))
Toogle = Left(MyObj.TextFrame2.TextRange.Characters.Text, 4)
TextName = Mid(MyObj.TextFrame2.TextRange.Characters.Text, 5, 100)
If Toogle = "Show" Then
MyObj.ShapeStyle = msoShapeStylePreset9
MyObj.TextFrame2.TextRange.Characters.Text = _
"Hide" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = False Then
Rows(RowNum).ShowDetail = True
End If
Else
MyObj.ShapeStyle = msoShapeStylePreset11
MyObj.TextFrame2.TextRange.Characters.Text = _
"Show" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = True Then
Rows(RowNum).ShowDetail = False
End If
End If
Range("C" & RowNum).Select
End Sub
The issue is the Rows(RowNum).ShowDetail is always TRUE, no matter if it's expanded or collapsed. I can remove the If section and set it to TRUE or FALSE using "Rows(RowNum).ShowDetail = False" or "Rows(RowNum).ShowDetail = TRUE". However, if the user has manually expanded or collapsed the row it causes an error (which freaks them out)
This question and answer seemed promising but Rows(RowNum).ShowDetail always seems to be TRUE
I put the MsgBox in there for error checking. I'll remove it in the final version.
Have you tried using Hidden property? Something like:
With Sheet1.Rows(5)
.ShowDetail = .Hidden
End With
Take note though that for you to use .ShowDetail method, you'll need to Group the rows first (needs to be in outline form).
True if the outline is expanded for the specified range (so that the detail of the column or row is visible). The specified range must be a single summary column or row in an outline.
Above code toggles hiding/unhiding a grouped row 5. You don't even need an If statement for the toggling. HTH.
I have a combobox in my worksheet with a dynamic search, the dynamic search is done by searching the typed letters in a list that is on another worksheet. The search is done by excel formulas. The list is a defined range and then the combobox listfillrange is set to the named range.
When I type the dropdown list opens, for certain items the combobox allows me to select from the list and for others the selection disappear as I select. I tried figure why do these items disappear. The list is long (10,000 items) so it kind of works slow, but I am not sure if this is the problem.
How can I fix such a problem? Is there a way to define a variable for the mouseclick selection from dropdown list?
Thanks in advance,
Tali
This is my code:
Private Sub ComboBox1_Change()
Sheets("PS").EnableCalculation = True
ComboBox1.ListFillRange = "DropDownList"
ComboBox1.DropDown
End Sub
Private Sub CommandButton21_Click()
Dim PS As Worksheet
Application.ScreenUpdating = False
Application.ErrorCheckingOptions.BackgroundChecking = False
Sheets("PharmaSoft").Select
Set PS = Sheets("PS")
SelectionA = PS.Range("J2").Value
If ComboBox1.Value = SelectionA Then
Range("J19") = "Pharmacy purchase price"
Range("N19") = PS.Range("K2")
Range("O19") = "ILS"
Range("J21") = "Pharmacy selling price Incl.VAT"
Range("N21") = PS.Range("L2")
Range("O21") = "ILS"
Range("J23") = "Package size"
Range("N23") = PS.Range("M2")
Range("J19:O23").Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Selection.Font.Bold = True
Application.ScreenUpdating = True
Range("N19").Errors.Item(1).Ignore = True
Range("N21").Errors.Item(1).Ignore = True
Range("N23").Errors.Item(1).Ignore = True
Else
MsgBox "Please select a product", vbCritical, "Error"
End If
Sheets("PS").EnableCalculation = False
End Sub
Private Sub CommandButton22_Click()
Application.ScreenUpdating = False
Sheets("PharmaSoft").Select
ComboBox1.Value = Null
Range("J19:O23").Value = Null
Application.ScreenUpdating = True
End Sub
Also the code for the workbook:
Private Sub Workbook_Open()
Sheets("PharmaSoft").Select
Application.ScreenUpdating = False
Sheets("PharmaSoft").ComboBox1.Value = Null
Range("J19:O23").Value = Null
Application.ScreenUpdating = True
End Sub
Although I can't comment on what you're doing given that your search is done as you mention with excel formulas. I do know that using data validation via a combo box can be pretty quick. The method I use is as per this page and is extremely fast. It's pretty much instantaneous on a validation range that is about 15k rows long. The best part about it is that it provides auto completion. So when you type in the combo box and that value isn't in the list, the entry that matched one character ago disappears. It's a good visual cue whether you're selection is valid or not. And of course, you can still use the drop down box in the usual way. The only down side is that, as coded at the link provided, you have to double-click to enter the auto-completion mode.
I want to make PivotItem.Visible = False but I keep getting an error:
Unable to set the Visible property of the PivotItem class
I tried all the solutions I found in the internet but none seems to work
Sub FloorCompareSetter()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim PivotSheet As Worksheet
Set PivotSheet = ThisWorkbook.Worksheets("PIVOT")
PivotSheet.PivotTables("PivotTable5").RefreshTable
Set pt = PivotSheet.PivotTables("PivotTable5")
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
Set pf = pt.PivotFields("Period")
For Each pi In _
pt.PivotFields("Period").PivotItems
Select Case pi.Name
Case Is = "1601A"
pi.Visible = True
Case Else
pi.Visible = False 'error
End Select
Next pi
End Sub
I tried refreshing the table and this line but still not working:
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
Here is a picture of my pivot table:
What am i doing wrong and how can I solve this problem ?
You will get this error if you attempt to hide all the items on any axis (rows, columns, filters). You can trap this error in your code by comparing the HiddenItems.Count property of your PivotField object to the PivotItems.Count property of the same object and make sure you are not trying to remove the last item of the collection from view:
So in your case statement, you can replace with update with something like:
Select Case pi.Name
Case Is = "1601A"
pi.Visible = True
Case Else
If pf.HiddenItems.Count < (pf.PivotItems.Count - 1) Then
pi.Visible = False
Else
MsgBox "Cannot hide all the items on this axis"
Exit For '<~~ break the loop to stop the MsgBox popping up
End If
End Select
Note that whilst manipulating the pivot table Excel won't allow you to remove the last item from an axis - the OK button will become disabled:
I added
On Error Resume Next
before
pi.Visible = False 'error
it works now !
The issue of the above solutions are: Before the loop reach the expected filter value, Robin's solution will stop (suppose the ideal value is the last item, but an unwanted value is the 1st one, and being the only one selected), while Sabir's solution (On Error Resume Next) can leave an unwanted value being selected.
How about before the loop starts, set the last item as visible? Like:
maxFilterIndex = objPivotField.PivotItems.Count
objPivotField.PivotItems(maxFilterIndex).Visible = True
Then at the end of the loop, if the expected value is not in the filter at all, we can leave the last one as visible, but pop a message box saying the value is not found.
With objPivotField
maxFilterIndex = objPivotField.PivotItems.Count
objPivotField.PivotItems(maxFilterIndex).Visible = True
For i = 1 To maxFilterIndex
If objPivotField.PivotItems(i).Name = filterValue Then
objPivotField.PivotItems(i).Visible = True
Else
'This is to skip the error. But the prob is it will leave the last quarter visible, if the last elelment not being ticked.
On Error Resume Next
objPivotField.PivotItems(i).Visible = False
If i = maxFilterIndex _
And objPivotField.HiddenItems.Count = (maxFilterIndex - 1) Then
MsgBox "The chosen filter values are not found, and EXCEL can't hide all the items on this Pivot filter"
End If
End If
Next i
End With
again I need little help which I will greatly appreciate.
Basically, on my dashboard page I have couple of checkboxes that control numerous of pivot tables in the background.
I have checkboxes that are called "definite", "tentative", "pending,", ... and also corresponds to values in pivot fields.
and I have numerous of pivot tables called: "Hidden_1" or "Hidden_2" in different sheets but all with the same structure.
My idea was that If someone checked "definite", it will be selected in all pivot pivot tables in fields called "Status". If someone "unchecked" this checkbox, the pivots will react.
To do so I used a code that I create before and it was working well:
Sub checkbox1()
Application.ScreenUpdating = False
On Error Resume Next
Dim pt As PivotTable, wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
For Each pt In wks.PivotTables
With pt
If .Name = "Hidden_1" Or .Name = "Hidden_2" Then
.PivotFields("Status").CurrentPage = "definite"
End If
End With
Next pt
Next wks
Application.ScreenUpdating = True
End Sub
However, this code selects only one value, so I can't have selected both "definite" and "pending" if someone checked those boxes. Right now all checkboxes has a separate code assigned where only .CurrentPage = "checkboxname" was changed..
I have two questions:
1) what is the best way to select multiple values. E.g. if checked boxes "definite" and "pending" are checked, pivot tables should have selected two values "definite" and "pending" selected in the "Status" field
2) what is the best way to "dis-select" the value? Right now, my procedure checkbox1 is running everytime that the checkbox is clicked. And I want it to run only when I am "checking" it.
Right now I am trying to link the checkbox with cell, e.g. "definite" has H10, so my code starts with the line:
If Range("H10").Value = True Then
'code to select the value in "Status" field
Else
'code to unselect the value in "Status" field
End If
I should also noted that I couldn't use ActiveX Checkbox because I had error: "cannot insert object" and I used form controls. I read that this error is somehow connected with a patch that I have installed.
Thank you all for your help,
Matt
I worked on it and found such a solution:
Sub checkbox1()
Dim choice1, choice2, choice3, choice4, choice5, choice6, choice7
Dim oPI As PivotItem
Dim pt As PivotTable, wks As Worksheet
If Sheets("Hidden").Range("B6").Value = "True" Then
choice1 = "Definite"
End If
If Sheets("Hidden").Range("B7").Value = "True" Then
choice2 = "Tentative"
End If
If Sheets("Hidden").Range("B8").Value = "True" Then
choice3 = "Hold/Option"
End If
If Sheets("Hidden").Range("B9").Value = "True" Then
choice4 = "Pending"
End If
If Sheets("Hidden").Range("B10").Value = "True" Then
choice5 = "Waitlist"
If Sheets("Hidden").Range("B11").Value = "True" Then
choice6 = "Lost"
End If
If Sheets("Hidden").Range("B12").Value = "True" Then
choice7 = "Cancelled"
End If
Sheets("Hidden_pivot1").PivotTables("Hidden_1").PivotFields("SalesStatus").ClearAllFilters
Sheets("Hidden_pivot1").PivotTables("Hidden_3").PivotFields("SalesStatus").ClearAllFilters
Sheets("Hidden_pivot2").PivotTables("Hidden_2").PivotFields("SalesStatus").ClearAllFilters
Sheets("Hidden_pivot2").PivotTables("Hidden_4").PivotFields("SalesStatus").ClearAllFilters
For Each wks In ActiveWorkbook.Worksheets
For Each pt In wks.PivotTables
With pt
If .Name = "Hidden_1" Or .Name = "Hidden_2" Or .Name = "Hidden_3" Or .Name = "Hidden_4" Then
For Each oPI In pt.PivotFields("SalesStatus").PivotItems
Select Case oPI.Name
Case choice1, choice2, choice3, choice4, choice5, choice6, choice7
Case Else
oPI.Visible = False
End Select
Next
End If
End With
Next pt
Next wks
End Sub
This work but is so slow. It would be better if the macro could add and delete those items, instead of re-creating the entire choice.