Pivot table - deselect less than a value - excel

On my pivot table (all items selected). I need to deselect everything lower than 11
(10,9,8,7,6,5,4,3,2,1,0,(blank),(none))
Im currently using (below)
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Count")
.PivotItems("10").Visible = False
.PivotItems("9").Visible = False
.PivotItems("8").Visible = False
.PivotItems("7").Visible = False
.PivotItems("6").Visible = False
.PivotItems("5").Visible = False
.PivotItems("4").Visible = False
.PivotItems("3").Visible = False
.PivotItems("2").Visible = False
.PivotItems("1").Visible = False
.PivotItems("0").Visible = False
.PivotItems("none").Visible = False
.PivotItems("(blank)").Visible = False
End With
... - it works, But if one of the values defined are not available, this doesnt work.
Is there an easier way to deselect items less than a particular value?
example:
= if "8" is on the list - then deselect it, else ignore.
or perhaps
= .PivotItem("<11").Visible = False

Try this
On Error Resume Next
For i = 10 To 0 Step -1
.PivotItems(i).Visible = False
Next i
On Error GoTo 0

Related

How to filter all of "string" out of a pivot table

I am trying to filter out all rows that include this string "PRC" out of my pivot table. I was doing this by recording a macro, but when I made the keystrokes, I got the code below that (in the macro recording) filtered each row out individually and not all rows that include "PRC". The problem with this is when I use this macro for the next report, it filters out only the exact same rows with the strings recorded and gives me an error message when the exact same row (exact string) is not included in the next report, and then further does not filter out the different rows (new ones). Please help:
With ActiveSheet.PivotTables("PivotTable7").PivotFields("Part Description")
.PivotItems("PRC,4214,2.2,16.5MB,CLX,L1").Visible = False
.PivotItems("PRC,4214Y,2.2,16.5MB,CLX,L1").Visible = False
.PivotItems("PRC,5215,2.5,13.75MB,CLX,L1").Visible = False
.PivotItems("PRC,6137,3.9,24.75MB,SKL,H0").Visible = False
.PivotItems("PRC,6146,3.2,24.75MB,SKL,H0").Visible = False
.PivotItems("PRC,6226,2.7,19.25MB,CLX,B1").Visible = False
.PivotItems("PRC,6230N,2.3,27.5MB,CLX,B1").Visible = False
.PivotItems("PRC,6246,3.3,24.75MB,CLX,B1").Visible = False
.PivotItems("PRC,7251,2.1,32M,ONP,120W,B2").Visible = False
.PivotItems("PRC,7281,2.1,32M,ONP,155W,B2").Visible = False
.PivotItems("PRC,7301,2.2,64M,ONP,155W,B2").Visible = False
.PivotItems("PRC,7351,2.4,64M,ONP,155W,B2").Visible = False
.PivotItems("PRC,7351P,2.4,64M,ONP,155W,B2").Visible = False
.PivotItems("PRC,7371,3.1,64M,ONP,200W,B2").Visible = False
.PivotItems("PRC,7401,2.0,64M,ONP,155W,B2").Visible = False
.PivotItems("PRC,7401P,2.0,64M,ONP,155W,B2").Visible = False
.PivotItems("PRC,7551,2.0,64M,ONP,180W,B2").Visible = False
.PivotItems("PRC,7601,2.2,64M,ONP,180W,B2").Visible = False
End With
You can check if the PivotItem consist of the text "PRC" by using Instr, like If InStr(PvtItm.Caption, "PRC") > 0 Then.
Modified Code
Option Explicit
Sub FilterPivotItems()
Dim PvtItm As PivotField
Dim PvtTbl As PivotTable
'Set the Pivot Table object
Set PvtTbl = ActiveSheet.PivotTables("PivotTable7")
' loop through Pivot-items
For Each PvtItm In PvtTbl.PivotFields("Part Description")
If InStr(PvtItm.Caption, "PRC") > 0 Then
PvtItm.Value = False
End If
Next PvtItm
End Sub

How to Hide certain userform fields during initialization?

So I have a userform that consist of Multipagesand one of the pages contain togglebuttons which hide and unhide fields on the Userform as well as on the excel worksheet. The picture below shows the togglebutton page.
The code for HAZOP/ SIL & LOPA is the same except which fields it hides is different. Below is the code for HAZOP togglebutton.
Private Sub togbHAZOP_Click()
If togbHAZOP = True Then
Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = False
Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = False
Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = False
Frame5.Enabled = True
Frame5.Visible = True
Frame6.Enabled = True
Frame6.Visible = True
Frame7.Enabled = True
Frame7.Visible = True
HazOp.Enabled = True
HazOp.Visible = True
Else
Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = True
Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = True
Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = True
Frame5.Enabled = False
Frame5.Visible = False
Frame6.Enabled = False
Frame6.Visible = False
Frame7.Enabled = False
Frame7.Visible = False
HazOp.Enabled = False
HazOp.Visible = False
End If
End Sub
Code for Initialization at the moment but it doesn't work, gives an error saying "Run-time error 438: Object doesn't support this property or method"
Private Sub UserForm_Initialize()
WizardProp.MultiPage1.Value = 0
Me.MultiPage1.Style = fmTabStyleNone
togbHAZOP.Frame5.Enabled = False
togbHAZOP.Frame5.Visible = False
togbHAZOP.Frame6.Enabled = False
togbHAZOP.Frame6.Visible = False
togbHAZOP.Frame7.Enabled = False
togbHAZOP.Frame7.Visible = False
togbHAZOP.HazOp.Enabled = False
togbHAZOP.HazOp.Visible = False
End Sub
The issue I am having is how do I hide certain fields within the userform at the beginning of the code and based upon the user's selection using the togglebuttonswill hide/unhide because at the moment I have to click on the togglebuttons then unclick to get it where the linked fields are hidden and doing that everytime during initialization can be a nuisance.
Note that you can reduce the code like below. Replace True with togbHAZOP.Value and False with Not togbHAZOP.Value in the If togbHAZOP = True part. So you don't need to repeat the whole code.
Private Sub togbHAZOP_Click()
Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = Not togbHAZOP.Value
Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = Not togbHAZOP.Value
Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = Not togbHAZOP.Value
Frame5.Enabled = togbHAZOP.Value
Frame5.Visible = togbHAZOP.Value
Frame6.Enabled = togbHAZOP.Value
Frame6.Visible = togbHAZOP.Value
Frame7.Enabled = togbHAZOP.Value
Frame7.Visible = togbHAZOP.Value
HazOp.Enabled = togbHAZOP.Value
HazOp.Visible = togbHAZOP.Value
End Sub
Since togbHAZOP is a toggle it does not have a frame togbHAZOP.Frame5 that's probably the issue here. It should probably be something like:
Private Sub UserForm_Initialize()
WizardProp.MultiPage1.Value = 0
Me.MultiPage1.Style = fmTabStyleNone
Me.Frame5.Enabled = False
Me.Frame5.Visible = False
Me.Frame6.Enabled = False
Me.Frame6.Visible = False
Me.Frame7.Enabled = False
Me.Frame7.Visible = False
Me.HazOp.Enabled = False
Me.HazOp.Visible = False
End Sub

When a checkbox is checked the multiple textboxes become visible however I need a second activity

I have working code to show and hide some text boxes etc, however I wanna have the text which is filled in one of those text boxes appears in a cell. Actually 2 text box values separate by a comma in one cell.
Private Sub CheckBox2_Click()
If CheckBox2 Then
ComboBox151.Visible = True
TextBox227.Visible = True
TextBox228.Visible = True
Label358.Visible = True
Label359.Visible = True
TextBox_combi.Visible = True
comments.Visible = True
Else
ComboBox151.Visible = False
TextBox227.Visible = False
TextBox228.Visible = False
Label358.Visible = False
Label359.Visible = False
TextBox_combi.Visible = False
comments.Visible = False
End If
End Sub

Excel - Set values of Userform Checkboxes based on cell contents

I'm developing a userform, one section of which contains three checkboxes referring to different parts of the world. Depending on the combination these enter a text value into cell C9.
I want to have the checkboxes reflect what is in the cell already when the user goes back into the userform. I've been able to do this for every other item in the userform (option buttons, textboxes, comboboxes), but my checkboxes don't respond at all, they are simply unchecked when the userform appears, regardless of C9's value.
The following code is in the userform_intialize module. Any ideas?
If wsM.Range("C9").Value = "EU-5" Then
NABox.Value = False And EUBox.Value = True And RoWBox.Value = False
ElseIf wsM.Range("C9").Value = "EU-5 & RoW" Then
NABox.Value = False And EUBox.Value = True And RoWBox.Value = True
ElseIf Sheets("Menu").Range("C9").Value = "NA & EU-5" Then
NABox.Value = True And EUBox.Value = True And RoWBox.Value = False
ElseIf wsM.Range("C9").Value = "North America" Then
NABox.Value = True And EUBox.Value = False And RoWBox.Value = False
ElseIf wsM.Range("C9").Value = "NA & RoW" Then
NABox.Value = True And EUBox.Value = False And RoWBox.Value = True
ElseIf wsM.Range("C9").Value = "Rest of World" Then
NABox.Value = False And EUBox.Value = False And RoWBox.Value = True
Else: NABox.Value = False And EUBox.Value = False And RoWBox.Value = False
End If
Thanks for any help.
Put the Me. keyword in front of the checkbox name.
May also be better to use a SELECT CASE statement instead of ElseIf.
NABox.Value = False And EUBox.Value = True And RoWBox.Value = False needs to be three separate commands. Either on separate rows, or split with a : (both examples in the code below).
Private Sub UserForm_Initialize()
With Me
Select Case wsm.Range("C9").Value
Case "EU-5"
NABox.Value = False
EUBox.Value = True
RoWBox.Value = False
Case "EU-5 & RoW"
NABox.Value = False : EUBox.Value = True
RoWBox.Value = False
Case "NA & EU-5"
Case Else
End Select
End With
End Sub
Edit - I don't think you need to explicitly declare the False tickboxes - they're False by default when the form opens.

How do I have one Userform event procedure (checkbox click) control multiple IF THEN statements?

Hey so I am a beginner with VBA but here is my dilemma:
Creating an excel userform with VBA -
I have a combobox with the numbers 1-8, the number of textboxes shown for the user to fill out is based off of the number selected in the combobox.
I also have a checkbox which, when "TRUE", enables the user to create a custom title for the output of the textbox.
Ex:
Number of Goals = 2
Custom Label Goals? [x] (True)
Goal 1 : $1,000,000
"New Home"
Goal 2 : $50,000
"Boat"
My issue is that I cannot get the number of custom label boxes to be the same as the number of goals selected. So when Number of goals is 2, 8 custom goal textboxes still show up if the checkbox is TRUE.
I found a way using the code below, but when I enter the second IF THEN statement, the first no longer responds to the clicking the checkbox:
Private Sub cbIMPLBL_Click()
If cboIMP.Value = "1" And cbIMPLBL.Value = "True" Then
txtIMPLBL1.Visible = True
txtIMPLBL2.Visible = False
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
Else
txtIMPLBL1.Visible = False
txtIMPLBL2.Visible = False
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
End If
If cboIMP.Value = "2" And cbIMPLBL.Value = "True" Then
txtIMPLBL1.Visible = True
txtIMPLBL2.Visible = True
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
Else
txtIMPLBL1.Visible = False
txtIMPLBL2.Visible = False
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
End If
End Sub
Any help here would be greatly appreciated.
As I am just beginning is there a different way I am supposed to be doing this, e.g. not writing all of this out individually, but as a control or function (not sure what the term would be) of some sort?
Thank you so much!!!!
Currently both if statements will fire. So if cboIMP.Value = "1" then it first runs through the first if statement and unhides the first text box. But it immediately runs the second if statement and because it does not = "2" it re hides the text box.
There are two choices a select case statement or else if statements.
Select Case:
If cbIMPLBL.Value = "True" Then
Select Case cboIMP.Value
Case 1
'hide/unhide for 1
Case 2
'hide/unhide for 2
Case Else
'Hide all
End Select
End If
Else if
If cbIMPLBL.Value = "True" Then
if cboIMP.Value = 1 then
'do your stuff
Else if cboIMP.Value = 2 then
'do your stuff
Else
'hide everything
end if
End If
This way once the code finds a true it ignores all others. If it does not find any true statements it runs the else.

Resources