How to Hide certain userform fields during initialization? - excel

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

Related

Concatenation and looping through Checkboxes & Boolean values

Below code pulls the values from 10 checkboxes and applies a respective Boolean check.
I'm struggling with concatenating the checkbox and Boolean names into a While loop. Anyone assist please?
If CheckD1.Value = True Then check1 = True
If CheckD2.Value = True Then check2 = True
If CheckD3.Value = True Then check3 = True
If CheckD4.Value = True Then check4 = True
If CheckD5.Value = True Then check5 = True
If CheckD6.Value = True Then check6 = True
If CheckD7.Value = True Then check7 = True
If CheckD8.Value = True Then check8 = True
If CheckD9.Value = True Then check9 = True
If CheckD10.Value = True Then check10 = True
You need to use an array Check(1 To 10) instead of individual variables check1 … check10 and something like UserForm1.Controls to access your checkboxes by a variable name:
Dim Check(1 To 10) As Boolean
Dim i As Long
For i = 1 To 10
Check(i) = UserForm1.Controls("CheckD" & i).Value
Next i
UserForm1 is the form your checkboxes CheckD1 to CheckD10 are in.
If you used Form Controls on a worksheet then it should be
Check(i) = (ThisWorkbook.Worksheets("Sheet1").Shapes("CheckD" & i).OLEFormat.Object.Value = 1)
Worked it out myself in the end, so posting the answer for others if they need.
Declared my check Booleans as an array with:
Dim check(1 to 10) as Boolean
Then looped through the checkboxes on the UserForm with:
For i = 1 To 10
If Me.Controls("CheckD" & i).Value = True Then
check(i) = True
End If
Next I
Simple for those that know, but something new learnt for today!

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.

Pivot table - deselect less than a value

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

Resources