How select multiple Pivot Items using formula in VBA from a Pivot Field - excel

I'm trying use VBA to select all organizations that have a H or N as their fourth character.
I've tried modifying code to select either just H or N and it works. Also this code works, if I reverse the signs by selecting everything that's not H or N.
Sub Select_H_and_N_Organizations_View()
Dim WS As Worksheet
Dim PT As PivotTable
Dim PF As PivotField
Dim PI As PivotItem
Dim blnCheck As Boolean
blnCheck = False
Set WS = ActiveWorkbook.ActiveSheet
If WS.PivotTables.Count > 0 Then
Set PT = WS.PivotTables(1)
PT.ManualUpdate = True
Set PF = PT.PivotFields("Organization")
For Each PI In PF.PivotItems
If Mid(PI.Name, 4, 1) = "H" Or Mid(PI.Name, 4, 1) = "N" Then
blnCheck = True
End If
Next PI
If blnCheck = True Then
Set PF = PT.PivotFields("Organization")
For Each PI In PF.PivotItems
If Mid(PI.Name, 4, 1) = "H" Or Mid(PI.Name, 4, 1) = "N" Then
If PI.Visible = False Then
PI.Visible = True
End If
End If
Next PI
For Each PI In PF.PivotItems
If Mid(PI.Name, 4, 1) <> "H" Or Mid(PI.Name, 4, 1) <> "N" Then
If PI.Visible = True Then
PI.Visible = False ''This is where code breaks
End If
End If
Next PI
Else
MsgBox "There are no H or N Organizations available.", vbOKOnly, "Pivot Tables"
End If
End If
PT.ManualUpdate = False
Set WS = Nothing
Set PT = Nothing
Set PF = Nothing
Set PI = Nothing
End Sub

You checked first, if there is any pivotitem fulfilling your criteria. That is correct, as at least one pivotitem must remain visible.
Then you switched each wanted pivotitem visible.
At last, you want to hide all unwanted. There you just have to change your condition from Or to And, as otherwise your condition is always met (all would be invisible, and the error occurs at the last pivotitem, as one must remain visible):
If Mid(PI.Name, 4, 1) <> "H" And Mid(PI.Name, 4, 1) <> "N" Then

Related

Pivot table to refresh active workbook, select Item "Period", clear all selection and choose 2 last values

After performing some operations this part of code goes to the sheet called "Overview", refreshes a huge pivot table - there is only one and it is called "PivotTable2" (actually, it doesn't refresh it and I don't know why). The part earlier that adds lines to the source works so after refreshing it manually everything works. Next, it chooses every cell with a value "Period" and what I need it to do is to clear filters in the selection "Period" and select two last values that pop up after the pivot table is refreshed.
ActiveWorkbook.Sheets("Overview").Activate
Dim pi As PivotItem
Dim pt As PivotTable
Dim pf As PivotField
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Set pf = pt.PageFields("PivotTable2").PivotFields("Period")
For Each pf In pt.PageFields
pf.ClearManualFilter
pf.ClearAllFilters
pf.EnableMultiplePageItems = True
pf.AutoSort xlAscending, pf.SourceName
pf.CurrentPage = pf.PivotItems(pf.PivotItems.Count).Name
If pf.CurrentPage = "(blank)" And pf.PivotItems.Count > 2 Then
pf.CurrentPage = pf.PivotItems(pf.PivotItems.Count - 2).Name
End If
If pf.CurrentPage = "(blank)" And pf.PivotItems.Count > 1 Then
pf.CurrentPage = pf.PivotItems(pf.PivotItems.Count - 1).Name
End If
Next pf
Next pt
End Sub
Run-time error '1004':
Unable to get the PageFields property of the PivotTable class
I would be happy if you point at a problem or suggest a solution. Thank you!
This code works, but instead of only working with the field "Period" as my code suggests it performs operations on all fields in a pivot table and chooses 2 last values in a filter.
Dim pi As PivotItem
Dim pt As PivotTable
Dim pf As PivotField
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Set pf = ActiveSheet.PivotTables("PivotTable2").PivotFields("Period")
For Each pf In pt.PageFields
pf.ClearManualFilter
pf.ClearAllFilters
pf.EnableMultiplePageItems = True
pf.AutoSort xlAscending, pf.SourceName
pf.CurrentPage = pf.PivotItems(pf.PivotItems.Count).Name
If pf.CurrentPage = "(blank)" And pf.PivotItems.Count > 2 Then
pf.CurrentPage = pf.PivotItems(pf.PivotItems.Count - 2).Name
End If
If pf.CurrentPage = "(blank)" And pf.PivotItems.Count > 1 Then
pf.CurrentPage = pf.PivotItems(pf.PivotItems.Count - 1).Name
End If
Next pf
Next pt

How to select the last item in a pivot filter for multiple pivot tables?

I need help with the below code and pivot table.
I run my script on a weekly basis and each time I need time select the last available item in the below pivot tables (PivotTable1, PivotTable2 and PivotTable3):
I tried the below code but it doesn't work:
Dim pi As PivotItem
Dim lLoop As Long
Dim pt As PivotTable
Dim lCount As Long
Dim lWeeks As Long
On Error Resume Next
lWeeks = 1
If lWeeks = 0 Then Exit Sub
Application.ScreenUpdating = False
Set pt = ActiveSheet.PivotTables("PivotTable1")
For Each pi In pt.PivotFields("Week").PivotItems
pi.Visible = False
Next pi
With pt.PivotFields("Week")
For lLoop = .PivotItems.Count To 1 Step -1
.PivotItems(lLoop).Visible = True
lCount = lCount + 1
If lCount = lWeeks Then Exit For
Next lLoop
End With
On Error GoTo 0
Application.ScreenUpdating = True
I also tried the below but it's still not working:
Sheets("Pivot").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
ActiveSheet.PivotTables("PivotTable1").PivotFields("ExtractDate"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("ExtractDate").CurrentPage _
= ThisWorkbook.Worksheets("Pivot").Range("B2").Value
In this case I'm having the Runtime Error 1004: Unable to get the PivotTables property of the Worksheet class.
Can you please advise how to modify the above codes to select the last available item in the 'Week' filter?
Also, how to modify this code to select the last value for these 3 pivot tables?
Thanks in advance.
You can set the current filter page with the name of the last (or first) PivotItem:
With ActiveSheet.PivotTables("PivotTable1").PageFields("Week")
.ClearManualFilter ' or ClearAllFilters
.AutoSort xlAscending, .SourceName
.CurrentPage = .Pivotitems(.Pivotitems.Count).Name
If .CurrentPage = "(blank)" And .Pivotitems.Count > 1 Then
.CurrentPage = .Pivotitems(.Pivotitems.Count - 1).Name
End If
End With
If the last entry is blank, it selects the previous one.
If you need the other end of your date range, just change xlAscending to xlDescending.
You can loop over all PivotTables in a worksheet and set each filter to the last page by this:
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
' Set pf = pt.PageFields("Week")
For Each pf In pt.PageFields
pf.ClearManualFilter ' or ClearAllFilters
pf.EnableMultiplePageItems = True
pf.AutoSort xlAscending, pf.SourceName
pf.CurrentPage = pf.Pivotitems(pf.Pivotitems.Count).Name
If pf.CurrentPage = "(blank)" And pf.Pivotitems.Count > 1 Then
pf.CurrentPage = pf.Pivotitems(pf.Pivotitems.Count - 1).Name
End If
Next pf
Next pt
At least 1 item has to remain visible, so you can't loop over all items and set them .Visible = False. A loop over all except the last PivotItem should work, but is too slow.
I added a .RefreshTable to refresh the data in your PivotTable. If there are still wrong informations, you can refresh the PivotCache of your workbook additionally:
Dim pc As PivotCache
For Each pc In ActiveWorkbook.PivotCaches
pc.MissingItemsLimit = xlMissingItemsNone
pc.Refresh
Next pc

Change module - one click to toggle button vba excel

I have the following code in a module in Excel, however i'd like to assign it to a toggle button.
Sub Weeks3Hide()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Weekly Hoist Hours")
wsLR = ws.Cells(Rows.Count, 7).End(xlUp).Row
For x = 14 To wsLR
'analyze date, see if it's 3 weeks or older
If ws.Cells(x, 7) <= Date - 21 Then
'hide
ws.Range("a" & x).EntireRow.Hidden = True
End If
Next x
End Sub
I've so far got this: However there is an error in it.
Private Sub ToggleButton4_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Weekly Hoist Hours")
WsLR = ws.Cells(Rows.Count, 7).End(xlUp).Row
If ToggleButton4.Value = True Then
For x = 14 To WsLR
'analyze date, see if it's 3 weeks or older
If ws.Cells(x, 7) <= Date - 21 Then
'hide
ws.Range("a" & x).EntireRow.Hidden = True
End If
Next x
Else
ws.Range(WsLR).EntireRow.Hidden = False
End If
End Sub
Would appreciate any help! Thanks in advance
This line ws.Range(WsLR).EntireRow.Hidden = False.
You cannot refer to range simply with a number. If you are trying to hide a row you can use ws.Rows(WsLR).EntireRow.Hidden = False or something like ws.Rows("A2:A" & WsLR).EntireRow.Hidden = False.
The first thing to do is unhide any hidden rows. I would then iterate over the cells and use Union to create a Range of target Rows. In this way, you can hide all the Rows at once.
Private Sub ToggleButton4_Click()
Dim cell As Range, Rows As Range
With ThisWorkbook.Sheets("Weekly Hoist Hours")
For Each cell In .Range(Cells(14, 7), .Cells(Rows.Count, 7).End(xlUp))
.Rows.Hidden = False
If Not .ToggleButton1.Value Then Exit Sub
If cell.Value <= Date - 21 Then
If Rows Is Nothing Then
Set Rows = cell.EntireRow
Else
Set Rows = Union(Rows, cell.EntireRow)
End If
End If
Next
Rows.Hidden = False
End With
End Sub

Change color of axis bars in an Excel pivotchart

I have this PivotChart in Excel 2016
As you can see there are two properties in the axis field: "Date" and "Category".
There are two possible values for "Category": ASC and SBT.
Right now the bars related to either values are of the same colors (Red and Blue).
I want that if the "Category" is SBT, the colors of the bars must be different (for example, yellow and green). How can I achieve that?
Thanks
Try this.
Sub test()
Dim obj As ChartObject
Dim cht As Chart
Dim pnt As Point
Dim Ws As Worksheet
Dim s As String
Set Ws = ActiveSheet
Set obj = Ws.ChartObjects(1)
Set cht = obj.Chart
With cht
.ApplyDataLabels
For Each pnt In .SeriesCollection(1).Points
With pnt.DataLabel
.ShowCategoryName = True
.ShowValue = False
End With
s = pnt.DataLabel.Text
If InStr(s, "SBT") Then
pnt.Format.Fill.ForeColor.RGB = RGB(255, 2255, 0)
End If
With pnt.DataLabel
.ShowCategoryName = False
End With
Next pnt
For Each pnt In .SeriesCollection(2).Points
With pnt.DataLabel
.ShowCategoryName = True
.ShowValue = False
End With
s = pnt.DataLabel.Text
If InStr(s, "SBT") Then
pnt.Format.Fill.ForeColor.RGB = RGB(29, 219, 22)
End If
With pnt.DataLabel
.ShowCategoryName = False
End With
Next pnt
End With
End Sub

Filtering Row based off checkbox

I have some VBA code that I was intending to use to filter a row instead of a column. I have some issues though that I can't seem to work out, at this point I have confused myself.
Sub FilterRow()
Dim b As Object, cs As Integer
Set b = ActiveSheet.CheckBoxes(Application.Caller)
With b.TopLeftCell
cs = .Row
End With
Dim rng As Range, cel As Range, totalRng As Range
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
For Each cel In totalRng
If cel.Row <> cs Then
cel.EntireColumn.Hidden = True
Else
cel.EntireColumn.Hidden = False
End If
Next cel
End Sub
The goal is to have the code check columns C:DX of that specific row for a value, if a value is NOT found then to hide that column. Ultimately eliminating any blank cells in that particular row.
So assuming FilterRow is the macro that you've assigned to the checkbox, the following will do what I think you're trying to do:
Sub FilterRow()
Dim b As Object, cs As Integer
Set b = ActiveSheet.CheckBoxes(Application.Caller)
With b.TopLeftCell
cs = .row
End With
Dim rng As Range, cel As Range, totalRng As Range
Set totalRng = Range("C" & cs & ":DX" & cs)
If b.Value = 1 Then
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
For Each cel In totalRng
If cel.Value = "" Then
cel.EntireColumn.Hidden = True
Else
cel.EntireColumn.Hidden = False
End If
Next cel
Else
' Set up autofilter here
totalRng.EntireColumn.Hidden = False
End If
End Sub
Since this macro gets triggered when the checkbox is either checked or unchecked, I changed the logic so that it unhides all the columns when the check box is unchecked (ie reverses what it did when checked).

Resources