If statement is not working in Excel VBA - 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

Related

VBA multiple option dialog box output

I created a user form with multiple options and now I want that the option the user selects is shown in a label under the button that calls the user form.I changed the caption in the text box under the button to resemble what should happen
However my options aren't working. Should I save the output in a global variable and then call it back to change the label and if so how do I do that? Or is it possible to just call the selection within the user form?
The code I was trying to run was this one to call the message box and then change the text box which is actually a label called "labelpage"
Private Sub CommandButton1_Click()
UserForm1.Show
If UserForm1.OptionButton1 = True Then LabelPage.Caption = "Company Restricted"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Strictly Confidential"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Public Information (does not need to be marked)"
End Sub
I also had this for each button click just to close them after selection, within the user form code.
Private Sub OptionButton1_Click()
OptionButton1.Value = True
Unload Me
End Sub
Private Sub OptionButton2_Click()
OptionButton2.Value = True
Unload Me
End Sub
Private Sub OptionButton3_Click()
OptionButton3.Value = True
Unload Me
End Sub
Is there just a tiny mistake of syntax or something like that or is this just completely wrong? Thank you in advance for your help.
The issue is that you are unloading the UserForm, meaning the controls are not available to you. The solution is to just hide the UserForm:
Private Sub OptionButton1_Click()
Hide
End Sub
Private Sub OptionButton2_Click()
Hide
End Sub
Private Sub OptionButton3_Click()
Hide
End Sub

ActiveX Combobox doesn't close automatically

I have an ActiveX Combobox in one of my main sheet which control/update a series of charts.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
End Sub
I noticed that when I click the ComboBox and select one of its content, it leaves a blue highlight on the selection. So to prevent that, I added:
Private Sub cmBoxSelect_DropButtonClick()
Application.ScreenUpdating = False
ActiveCell.Activate
Application.ScreenUpdating = True
End Sub
It successfully removed the highlight.
However, it has a weird drawback. cmbSelect doesn't close automatically once user didn't select anything (once the combobox is active and the user click any cell in the sheet, it doesn't close out). It was working before I added the DropButtonClick event.
Did I missed anything or any wrong steps above? Thanks for your inputs!
EDIT#1
Seems I already found a solution by trial and error. I only added a blank Label and select it to remove the focus out of the ComboBox whenever there is a change. I also changed the DropButtonClick to LostFocus.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_LostFocus()
ActiveCell.Select
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
Me.Label1.Select
End Sub
You need to put the SelLength to 0 in multiple events to avoid highlighting:
so:
Me.cmBoxSelect.SelLength = 0
in:
Private Sub cmBoxSelect_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_LostFocus()
Private Sub cmBoxSelect_DropButtonClick()
Private Sub cmBoxSelect_Change()
Private Sub cmBoxSelect_GotFocus()
(you could add also Me.cmBoxSelect.SelStart = 0 )
Lets try this:
Not Event-triggered by a change, but by the dropbuttonclick
Private Sub changingComboBox(String s)
'series of codes which manipulates the charts, based on selection...
End Sub
Private Sub cmBoxSelect_DropButtonClick()
Dim s As String
s = cmBoxSelect.SelText
If (cmBoxSelect.SelText = cmBoxSelect.Value) Then
cmBoxSelect.Value = ""
cmBoxSelect.Value = s
Else
call changingComboBox(cmBoxSelect.Value)
End If
End Sub
How about that ?

Prevent OptionButton preselection in UserForms

I have the following code in a UserForm:
Private Sub CommandButton1_Click()
Warning_Box.Hide
End Sub
Private Sub OptionButton1_Click()
KeepGoing = 0
End Sub
Private Sub OptionButton2_Click()
KeepGoing = 1
End Sub
The first time this code runs, it works fine as none of the OptionButtons is preselected by the VBA code. The second time however, the previous choice is preselected. This appears to stop the correct setting of the KeepGoing value (as an earlier section sets KeepGoing = 2).
Basically, how do I make it so no OptionButton is preselected after the first run?
You will need to change
Warning_Box.Hide
to
Unload Me
You may have good reasons not to use Unload Me. If so, you can just set values of all option buttons to false somewhere in your code.
Warning_Box.OptionButton1.Value = False
for every option button. You can also loop trough Warning_Box.Controls and set value to false if it is option button.
Dim con As Control
For Each con In ufMain.Controls
If TypeName(con) = "OptionButton" Then con.Value = False
Next con

open a sheet checkbox is checked with vba excel

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

Remove shadow from ActiveX Option Button in Excel 2010 VBA

I was trying to emphasis the option button from activeX control when it is selected by user. I decided to show the shadow when it is selected and then hide the shadow when user selects other option button. The first process is working whereas the shadow cannot be removed even though I select other button. My VBA code is shown below:
Private Sub OptionButton1_Click()
OptionButton1.Shadow = False
If OptionButton1.Value = True Then
OptionButton1.Shadow = True
Else
OptionButton1.Shadow = False
End If
End Sub
Can anyone please help me to solve this?
In case of FORMS buttons you can use
Sub RemoveFormsButtonShadows()
'A.Leine 21/10/2015
For Each Button In ActiveSheet.Shapes
Button.Select
Selection.ShapeRange.Shadow.Visible = False
Next Button
End Sub
For that you have to create one sub which needs to be called from all the option buttons that you have. This common sub will simply remove the shadow from all option buttons. Here I am taking the example of 3 option buttons.
Option Explicit
Private Sub OptionButton1_Click()
RemoveShadow
If OptionButton1.Value = True Then _
OptionButton1.Shadow = True
End Sub
Private Sub OptionButton2_Click()
RemoveShadow
If OptionButton2.Value = True Then _
OptionButton2.Shadow = True
End Sub
Private Sub OptionButton3_Click()
RemoveShadow
If OptionButton3.Value = True Then _
OptionButton3.Shadow = True
End Sub
Sub RemoveShadow()
Dim objOpt As OLEObject
With ActiveSheet
For Each objOpt In .OLEObjects
If TypeName(objOpt.Object) = "OptionButton" Then
objOpt.Shadow = False
End If
Next
End With
End Sub