Select multiple values from a slicer - excel

I have a pivot table with a slicer. The slicer has 50+ values in it and these values can change. I use the slicer to filter the data - I am always looking for the same 5 values, and my 5 values start with the same text string.
I had recorded a macro to select my 5 values. this macro selects the items that are true and then lists all the other values and sets selection as false.
When new values are added the macro crashes because I don't have an explicit line of code to set the selection of the new value to false.
I found code to select one value from the slicer and deselect all other values without having to list them all explicitly, but I can't find code to find the 5 values and deselect all other values without having to list them explicitly, or
a way to modify this code to select all slicer items that "contains" the consistent text string. either would help...
this is the code I have to find one specific value, but deselect the rest without stating them explicitly:
For Each slcCache In ActiveWorkbook.SlicerCaches
slcCache.ClearManualFilter
Next
With ActiveWorkbook.SlicerCaches("Slicer_Fruit")
For Each oSlicerItem In .SlicerItems
If oSlicerItem.Name = "abcx Apple" Then
oSlicerItem.Selected = True
Else
oSlicerItem.Selected = False
End If
Next oSlicerItem
End With
How can I change the code to select the slicer items that starts with "abcx" or contains this text?
or is there a way to select "abcx Apple" and "abcx Pear" and "abcx Banana" but not select any other values?

Try this :
Sub Slicer_select()
ActiveWorkbook.SlicerCaches("Slicer_Fruit").ClearManualFilter
Dim Sl_I As SlicerItem
For Each Sl_I In ActiveWorkbook.SlicerCaches("Slicer_Fruit").SlicerItems
If Not Sl_I.Value Like "abcx*" Then Sl_I.Selected = False
Next
End Sub

Related

Hide blank or Null Field Options in Pivot Table

I am trying to edit the code below to hide the (blank) values in the field options list. I think because I am using a data model, I am getting a little tripped up with the syntax. I am getting the error message, "Unable to get the PivotItems property of the PivotField class"
Sub hi()
With ActiveSheet.PivotTables("PivotTable3").PivotFields("[Range 2].[Goal].[Goal]")
.PivotItems("(blank)").visible = False
End With
End Sub
I think you may be able to solve for this by selecting the Pivot Table entirely and attempting to find all of the blanks from the data that is being pulled into the Pivot.
Select a column in your worksheet that contains blank rows.
Press Ctrl+F on the keyboard to open the Find & Replace box.
Select theFind tab and click on Options to open the advanced menu. Don’t input anything in the Find what box.
Check the box beside Match entire cell contents.
Select Sheet from the drop-down menu of Within.
Select Values from the drop-down menu of Look in.
Click on Find All button to get all the blank cells in the selected range.
Select all these rows by Ctrl+A.
Close the Find box.
Delete the rows by clicking on the right button, and selecting the Delete option.
I hope this helps! You can view the whole article that has screenshots on how to do this here.
I could suggest adding an slicer where the default view could be an unselected option for the blanks values.
Dim slcCache as SlicerCache '' Object to manipulate the slicer
Set slcCache = ActiveWorkbook.SlicerCaches("SlicerName") '' Assign object
With slcCache
.ClearManualFilter
.SlicerItems("(blank)").Selected = False '' False value to unselect
'' I do not remember if this is the actual name for an empty value,
'' but you could find it or replace it by the index that should be 1
End with

Adding msgbox if my pivot table has empty cells

enter image description hereI have an Excel workbook that runs a macro and in the end displays everything in a pivot table
I am trying to add a msgbox when my pivot table has an empty cell (at the end of my macro)
I already did the conditional formatting version(color blank cells red) and it works fine, but the client wants a msgbox to alert him.
I found a IsEmpty command that should work but I cant seem to make it look only inside said pivot table.
Here is what I tried:
Sub IsEmpty()
If IsEmpty(ActiveSheet.Range("PivotTables(1)")) = True Then
'Cell A2 is not blank
MsgBox "Cell A2 is empty"
Else
End If
End Sub
I'm sure the way my If statement is written is false. Just cant seem to find the right syntax.
Thanks in advance
Picture added; I want the macro to target pivot's C column. However, you cant know which cell will be empty or how long the list will continue.
And if just make Excel check a broad spectrum(c2:c300), there will always be en empty cell after the pivot table is finished.
There might be a loop you can create but its way over my current skill set.
The pivot table's name is "PivotTable2"
Is there a to search only in the pivot table's column c for empty cells?
Please consider the capabilities of the function below. It will return True or False depending upon the count of items in columns A and C. In column A text, number or function is counted. In column C there must be a number in each cell. If both counts are the same the function returns True. Either count can be further refined if there are exceptions which the current function doesn't correction evaluate.
Function IsComplete() As Boolean
' 025
Dim Rng As Range
With ThisWorkbook.Sheets("PivotTables(1)") ' change to suit
' set a range from A3 to end of column A
Set Rng = .Range(.Cells(3, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With
With Application.WorksheetFunction
IsComplete = (.CountA(Rng) = .Count(Rng.Offset(0, 2)))
End With
End Function
You can use code to call this function and display a message box for True and another one if the function returns False. The code can be hitched to a button on your sheet, anywhere in your workbook.
Sub CommandButton1_Click()
Dim Txt As String
If IsComplete Then
Txt = "The pivot table was created without mistakes."
Else
Txt = "Some data are missing in the pivot table."
End If
MsgBox Txt, vbInformation, "Action report"
End Sub
You can also call the function as a UDF and display True or False in a cell of any sheet in your workbook. The UDF call would be like =IsComplete(). You might also embed the UDF call in an IF condition and display another text on your worksheet.
=IF(IsComplete(),"All's well","Missing Item")
In fact, you could slightly modify the function to return the difference between one count and the other and display "1 item missing". The possibilities are endless because the function is so versatile.

Grouping Sets of Rows with a toggle.button

I'm attempting to hide groups of rows with a toggle button. In this instance, Rows 15 through 20, 22 through 25, 27 and finally 30 through 32.
The code I have so far works as intended.
Private Sub ToggleButton5_Click()
Dim xAddress As String
xAddress = ("15:20")
If ToggleButton5.Value Then
Application.ActiveSheet.Rows(xAddress).Hidden = True
ToggleButton5.Caption = "Show Assets"
Else
Application.ActiveSheet.Rows(xAddress).Hidden = False
ToggleButton5.Caption = "Hide Assets"
End If
End Sub
How do I add multiple groups to this row?
I tried
xAddress = ("15:20,22:25")
xAddress = "15:20,22:25"
xAddress = ("15:20 And 22:25")
and I tried individually
xAddress = ("15,16,17,18,19,20,22,23,24,25")
This last line works somewhat but runs into errors if more than maybe six row numbers are cited (going from memory on past attempts).
If you need a "toggle", then consider the implementation of a "radio-button-logic". It is either on or off, thus if it is not Hidden it should be Hidden and vice versa. Usually it is 1 line only:
Sub ToggleRowsVisibility()
With ThisWorkbook.Worksheets(1).Range("15:20,22:25")
.EntireRow.Hidden = Not .EntireRow.Hidden
End With
End Sub
In the case of the code, it can be outside the If condition:
Application.ActiveSheet.Rows(xAddress).Hidden = ToggleButton5.Value
Use Range instead of Rows.
Application.ActiveSheet.Range(xAddress).Hidden = True
If you are using Range, make sure that the row reference is in the form row:row, e.g. 1:1, 2:2, 3:3 and not 1, 2, 3.
I generally steer clear of Rows. For example,
Debug.Print Rows("1,2,3").Address
returns
$123:$123
Not what you expect, right?
Requirements:
To hide/unhide a multiple selection (i.e. non-contiguous or multiple areas) of rows by the click of a button.
Target rows: [15:20], [22:25], [27] and [30:32] (row 27 included to show a mixed of rows combination).
Non VBA Solution:
This can be achieved without VBA. Use the Range.Group method (Excel) manually to group the rows. Unfortunately, this method cannot be applied at once to a multiple selection, so you’ll have to apply the method to each range of contiguous rows separately.
Select the first range of rows to be grouped (i.e. [15:20])
In the Excel menu, click the [Data] tab then in the [Outline] group, click the [Group] option in the [Group] dropdown menu.
The rows selected are now grouped with a button beside the rows heading. Use this button to toggle the visibility of the respective grouped rows.
Repeat the action for the remaining group of rows.
The advantages of this method compared with the VBA method are:
The grouped rows are fixed, even if new rows are added or deleted. With the VBA method the rows are “hard-coded” and will lose focus when rows are inserted\deleted.
The visibility of the Group of rows can be toggle all at once using the buttons located at the top\left angle, i.e. 1 to hide and 2 to unhide.
The visibility of the Group of rows can be toggle independently from each other using each group's button located in the rows heading. Vba would required either independent buttons or additional variables.
VBA Solution:
If you must use VBA then I suggest to use the correct syntax for multiple selection:
For a single row use: Rows(15).Select or Range("15:15").Select
For a range of contiguous rows use: .Rows("15:20").Select or Range("15:20").Select
For multiple selection of rows use the Range method as the Rows method doesn't work with multiple areas and when applied to a multiple selection returns only the rows of the first area.
For multiple selection of single rows use: .Range("30:30,32:32,34:34,36:36,39:39").Select
For multiple selection of non-contiguous rows use: .Range("15:20,22:25,27:27").Select
Proposed VBA Solution:
Following the above your code should be:
Private Sub ToggleButton5_Click()
Dim xAddress As String
xAddress = "15:20,22:25,27:27,30:32" 'Update as required
With ToggleButton1
Me.Range(xAddress).Rows.Hidden = .Value
.Caption = IIf(.Value, "Show Assets", "Hide Assets")
End With
End Sub

Excel Insert Rows - Using Named Ranges To Hide Rows

I have hidden rows with checkbox macros
Every time I want to insert a new row, the vba code gets messed up and the code assigned to hide rows below will not work
is there a way to dynamically have the rows change the code in the vba
So I don't have to redo all the row values and check boxes that move in the process
[IMG]http://i39.tinypic.com/2akek5z.jpg[/IMG]
Sub CheckBox1_Click()
If Range("B3").Value = True Then
Rows("4:18").EntireRow.Hidden = False
Else
Rows("4:18").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox2_Click()
If Range("B51").Value = True Then
Rows("52:66").EntireRow.Hidden = False
Else
Rows("52:66").EntireRow.Hidden = True
End If
End Sub
I have about 10 subcheckboxes in the code
If I insert or delete a row the code below will not work.
This is because the rows have changed
from the original code that I wrote
Edit
Use 'Named Ranges' instead of hard coding the range. The named range will automatically update when you insert a new row. For example:
I selected rows 9 thru 13 and named them 'RowsABC' as shown here:
I also named my checkbox cell as 'CheckABC' so that it is no longer dependent on what row it's in.
Your code now looks like this:
If Range("CheckABC").Value = True Then
Range("RowsABC").EntireRow.Hidden = False
Else
Range("RowsABC").EntireRow.Hidden = True
End If
Now you can select a row such as row 10 and right click -> insert, and your macro will hide rows 9:14 instead of 9:13 because the named range will update automatically when you insert.

Removing blank Excel Pivot Table entries

I have a pivot table based on PowerPivot with multiple levels in the Rows section. Quite a few of the elements are blank. Is there a way to suppress the blank rows?
My Pivot Table looks like this at the moment.
Product1
Release 1
Iteration 1
(blank)
Iteration 2
(blank)
Release 2
(blank)
(blank)
Product2
(blank)
Product3
Release 1
Iteration 1
Sprint 1
(blank)
(blank)
(blank)
This is what I want it to look like
Product1
Release 1
Iteration 1
Iteration 2
Release 2
Product2
Product3
Release 1
Iteration 1
Sprint 1
That's just an example. I've tried to set filters for each level not to display blanks but if I filter each level to hide blank fields the pivot table doesn't have any items in it. It seems that Excel ends up filtering each level out that has ANY blank values.
Not sure if I made sense or provided enough information for troubleshooting. I'm pretty much brain dead at the moment so I apologize.
Please let me know if it doesn't make sense or if I can provide any additional information to clarify what I'm trying to do and encountering.
Thanks.
EDIT: Changed the code block to be more clear and added an "after" code block to show what I want to get to. I guess the issue is that the "depth" of the rows for the entire pivot table has to be equal. For example if I have 3 indents for the first item, the others must also show 3 levels of indent worth of data. If I hide blanks and that results in 1 indent worth of data for the first item, it will hide non-blanks for other items as well if they appear after 1 indent. Still not sure if that makes any sense :) I need some sleep.
I think you should be able to check Show items with no data on rows and/or Show items with no data on columns in Display under PivotTable Options.
I ended up using a Flattened PivotTable instead from the PowerPivot "Manage" screen instead. That seemed to do what I needed.
I needed to solve exactly the same problem, but wanted the the rows to be hidden automatically when the pivottable filtering was changed or the data was refreshed, so I wrote a function called from a 'Worksheet_PivotTableUpdate' event on the sheet holding the pivottable, although you could drive it off a button if you prefer I suppose. This initially did the job by scanning down each row in the pivottable, hiding it if it was visible and didn't need to be (as the row's first cell was '(blank)'), or showing it if it was hidden and shouldn't be, otherwise just leaving it hidden or visible as it was.
However, I found the function ran very slowly, so here's my next attempt, which initially un-hides all the rows in the pivottable, then goes and finds all the blank cells in the first column, adding them to a range object, then it hides the entire row for every cell in that range. This version works about ten times as fast :-)
Pass in the name of the sheet where the pivottable reside, the name of the pivottable and an optional string to look for in the rows that need to be hidden; this defaults to '(blank)' if not supplied by the calling function.
Apologies in advance if I fall foul of anybody's variable or function naming conventions (I'm only a hacker ;-))
Sub FixBlankPivotRowsV2(ByVal SheetName As String, ByVal PivotName As String, Optional HideDefn as String = "(blank)")
Dim CurrPivot As PivotTable
Dim CurrRow As Range
Dim BlankRange As Range 'NB This is where we'll build the range of things to hide, which will initially be Nothing, as we never initialise it.
Dim oldStatusBar As Boolean
'Show a nice message in the status bar
oldStatusBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
Application.StatusBar = "Hiding blank entries in " & SheetName & ", back in a sec..."
Application.ScreenUpdating = False
'Get the pivottable to work with
Set CurrPivot = ActiveWorkbook.Sheets(SheetName).PivotTables(PivotName)
'Unhide all of the pivot rows first
CurrPivot.RowRange.Rows.EntireRow.Hidden = False
'Loop around each row in the pivottable
For Each CurrRow In CurrPivot.RowRange.Rows
If CurrRow.Offset(0, 0).value = HideDefn Then
If BlankRange Is Nothing Then
'This is the first blank row we've found, so just set the range up to be the first cell of that range
Set BlankRange = CurrRow.Offset(0, 0)
Else
'Add the newly found blank row to the range using a Union
Set BlankRange = Union(BlankRange, CurrRow.Offset(0, 0))
End If
End If
Next
'Only hide things if there's anything to hide!
If BlankRange Is Nothing Then
Debug.Print "Nothing to hide!"
Else
BlankRange.EntireRow.Hidden = True
End If
'Set the status bar back the way it was
Application.ScreenUpdating = True
Application.StatusBar = False
Application.DisplayStatusBar = oldStatusBar
End Sub
I looked for a way to select all of the 'blanks' cells in the first column using one of the Go To Special type functions, but couldn't find anything that would fit the bill, so please let me know if you know how to do such a thing as it will speed this up even more.
I hope this helps somebody out there!
Try to select the a cell in the pivot table "blank" press delete button and press spacebar.
No idea why or how this works, but I sleclected one of the cells in the PivotTable that contained (blank), then hot spacebar. I then hit the return key, and all the (blank) cells in the same column went blank. Repeated this for the other columns in the table.

Resources