open a sheet checkbox is checked with vba excel - vba

how I can check if a checkbox is selected ?
And after selecting a checkbox for example "a" (in my example) I want to open a excel sheet.
How can I solve this problem?
Thank you all.

In the UserForm module you can just place the following code, when your CheckBox is named "CheckBox1":
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
Worksheets("Sheet1").Visible = True
Else
Worksheets("Sheet1").Visible = False
End If
End Sub
This will make "Sheet1" visible when it's checked and invisible when unchecked.
If you name your checkbox differently, you'll see that if you double click the checkbox in the Userform Design, the VBE will already come up with
Private Sub CheckBoxName_Click()
End Sub

Related

If statement is not working in Excel VBA

Iam trying to create a simple if statement in Excel with VBA.
I'm creating a new checkbox
Adds the following code to the box.
Sub CheckBox1_Click()
HideRows "2:5"
End Sub
Sub HideRows(rowRange)
If CheckBox1 = False Then
Rows(rowRange).EntireRow.Hidden = True
Else: Rows(rowRange).EntireRow.Hidden = False
End If
End Sub
Result: The rows are hidden both if the checkbox is checked or unchecked.
(checkbox is checked)
All rows are visible
Uncheck the checkbox
Result: All rows are hidden
(checkbox is unchecked)
All rows are visible
Uncheck the checkbox
Result: All rows are hidden
You want it in a Change Event.
You do not need the If Then. CheckBox1 rturns a TRUE/FALSE, just use that.
And the EntireRow is also not needed when refering to Rows(). You are already refering to the whole row.
Also, it is good practice to always declare the parent to any Range Object, which Rows() is. If the the code is in the Worksheet code then use Me as it will refer to itself. If the code is in a module then use ActiveSheet or more preferably the specific sheet, Worksheets("Sheet1") :
Private Sub CheckBox1_Change()
HideRows "2:5"
End Sub
Sub HideRows(rowRange)
'if this code is not in the worksheet code then change `Me` to `ActiveSheet`
Me.Rows(rowRange).Hidden = Not CheckBox1
End Sub
Assuming its an ActiveX CheckBox, place this code in Sheet Module...
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
Rows("2:5").Hidden = True
Else
Rows("2:5").Hidden = False
End If
End Sub
Edit:
Or just use this...
Private Sub CheckBox1_Click()
Rows("2:5").Hidden = CheckBox1
End Sub
You can put this in one sub, assuming its an activeX checkbox
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
[2:5].EntireRow.Hidden = False
Else: [2:5].EntireRow.Hidden = True
End If
End Sub

Excel: Deselect all radio buttons in a group

I have a survey spreadsheet has a group of option buttons. If the user hasn't chosen one, I want them all to be deselected (or set to that greyed-out setting you can have in "TripleState"). If the user has chosen one, I'd still like to be able to clear them all (in case, for instance, he clicks one by mistake and isn't ready to answer yet.)
I know I can add another button as a "No Answer" choice, but on this sheet it would be a little inelegant.
Would it be simpler to use checkboxes instead, and make it so checking each box deselect the others (like radio buttons)? Deselecting would be the trivial part.
EDIT: Note the buttons are on a worksheet, not a userform.
I couldn't quite find a solution regarding option boxes however the following will work for check boxes:
Private Sub CheckBox1_Click()
CheckBox2.Value = False
CheckBox3.Value = False
End Sub
Private Sub CheckBox2_Click()
CheckBox1.Value = False
CheckBox3.Value = False
End Sub
Private Sub CheckBox3_Click()
CheckBox1.Value = False
CheckBox2.Value = False
End Sub
The above assumes 3 checkboxes for each survey selection. Just rename CheckBox1 CheckBox2 CheckBox3 to whatever you like, but remain consistent. The user will have to deselect every option they want to change however but will allow them to deselect all options and also embeds the option box behavior of only allowing one choice at a time.

Initializing a userform with preset values?

Working in VBA for excel 2010. This is my first time working with VBA and userforms. Right now I have a barebones userform "UserForm1" trying to sort this issue out. It consists of two radio buttons "OptionButton1" and "OptionButton2" belonging to GroupName "WK" and two textboxes "TextBox1" and "TextBox2".
When I run the userform, I want "OptionButton1" to be selected and the subsequent if/then statements to be run. However, right now I cannot get it to do this. My code:
Public Sub UserForm1_Initialize()
UserForm1.Show
Me.OptionButton1.Value = False
Me.OptionButton1.Value = True
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End Sub
Public Sub UserForm1_Activate()
End Sub
Public Sub OptionButton1_Click()
If Me.OptionButton1.Value = True Then
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End If
End Sub
Private Sub TextBox1_Change()
End Sub
When I run the form, nothing happens and "OptionButton1" is false. If I click it, the message box displays and the textbox displays the text. It just won't do it on startup. I have tried going into the optionbutton properties and setting 'value' to true, which makes it true on startup, but the messagebox still doesn't display and the textbox is blank until I click it.
...please help. Thank you.
I figured it out.
I suddenly found the dropdowns. Apparently I should have put Userform_Initialize() instead of UserForm1_Initialize(), and instead of OptionButton1_Click() I put the code into OptionButton1_Change() which ran the subsequent initialization sequence.
You guys/gals are awesomesauce. I have learned everything from reading your threads. Thank you!

Userform not triggering Initialize or Activate event

I kept a userform control button in my worksheet to fire up a macro, which in turn shows a user form, In the form I wish to display the opened files in checkboxes(using the Workbooks collection).I wish to run a macro that performs action for the user selected files only.
So for the button in my worksheet, I have assigned the following macro
Private Sub Button2_Click()
Load MyForm
MyForm.Show
End Sub
At first I kept the below code in the module where my macro sub is there.Since it's not working, I right clicked on user form and selected view code and kept the below code there.But still it's showing the same static designed user form, not the dynamic.I kept breakpoint at both load Myform and MYform.Show() and I stepped through code.It never went into intialize or activate method at all.
Private Sub MyForm_Activate()
'for checking the whether this method is called or not I am trying to change caption
MyForm.LabelSelectFile.Caption = "dhfdfldkfldzjf;zdfkz;d"
Dim mymyWorkBook As Workbook
For Each mymyWorkBook In Workbooks
'code for creating checkbox based on the file name displayed by the workbook collection
Next mymyWorkBook
End Sub
I can't understand why that event is not getting triggered.Please help me to overcome this.Thanks in advance
Even though the name of the form is MyForm, you still need to use userform.
'~~> in your worksheet
Private Sub Button2_Click()
MyForm.Show
End Sub
'~~> In the userform code area
Private Sub UserForm_Initialize()
'~~> Your code here
End Sub
or
Private Sub UserForm_Activate()
End Sub
The best is to always select the event from the drop down, rather than typing it

check a checkbox in word using vb.net

I have a word doc with several checkbox fields. I can fill the text fields, but haven't figured out how to check checkboxes.
I can't just make a macro in word and see how word does it because to use the keyboard to check the box (space bar), you have to enable document protection, which disables macro creation.
I just tried setting an old fashion Word2003 Checkbox with VBA. Worked with that piece of code:
' demo purposes - added a command
Private Sub CommandButton1_Click()
' FormFields refers to Word2003 FormFields
If ActiveDocument.FormFields(1).Type = wdFieldFormCheckBox Then
ActiveDocument.FormFields(1).CheckBox.Value = True
End If
' ContentControls refers to >= Word2007 Controls - thx to StevenDotNet for the hint
ActiveDocument.ContentControls(1).Checked = True
End Sub
On the other hand I created a VS2012 WordProject with VB.net and added some code to check the box on load.
Private Sub ThisDocument_Open() Handles Me.Open
Me.FormFields(1).CheckBox.Value = True
End Sub
Each checkbox has a Value property. You can set this property to True or False to check or uncheck the checkbox.
EDIT
I wrote a little macro that ticks all boxes in the active document. You can edit it to suit your needs. VBA is really nasty though, it took me around 15 minutes to figure this out.
Sub CheckAllBoxes()
Dim ctrl As ContentControl
For Each ctrl In ActiveDocument.ContentControls
If ctrl.Type = wdContentControlCheckBox Then
ctrl.Checked = True
End If
Next
End Sub