I asked a similar question this morning and i got a great answer, but then i pushed a little bit further and this is where i'm kinda stuck.
This is my initial post : Unselect an entire optionbutton group if another optionbutton outside the group is selected
But now, I would like to have this:
If OptionButton1.Value = True or OptionButton2.Value = True then Grey out OptionButton4 to OptionButton11 (Also gave it a group name : "Category").
But, If OptionButton3.Value = True, then it ungreys the OptionButton4 to OptionButton11 (Group Category).
EDIT:
I did something and it worked but once I run my userform and generate a number (userform purpose), the optionbuttons stay greyed out. Here's the code i used :
Private Sub OptionButton1_Click()
OptionButton4.Enabled = False
OptionButton5.Enabled = False
OptionButton6.Enabled = False
OptionButton7.Enabled = False
OptionButton8.Enabled = False
OptionButton9.Enabled = False
OptionButton10.Enabled = False
OptionButton11.Enabled = False
End Sub
Private Sub OptionButton2_Click()
OptionButton4.Enabled = False
OptionButton5.Enabled = False
OptionButton6.Enabled = False
OptionButton7.Enabled = False
OptionButton8.Enabled = False
OptionButton9.Enabled = False
OptionButton10.Enabled = False
OptionButton11.Enabled = False
End Sub
EDIT 2:
My solution for now is to add
Unload Me
UserForm1.Show
so it reset my userform and remove the greyed optionbuttons
That looks like you solved your own issue. Another would be to create a validation sub that would actually check everything every time an option is selected. Also there is a way to access all the forms controls something like Forms('myform').controls('mycontrol') I admit I am rusty but if you iterate through you can disable enable as wanted and call that on load
Instead of unloading and reloading the form, handle the enabling/disabling.
Create a common procedure to handle the enabling/disabling so that you do not have to duplicate the code.
You can loop through all the controls which will not only reduce the lines of the code but will be easier to maintain.
Is this what you are trying?
Option Explicit
Private Sub OptionButton1_Click()
EnableOptBtns False
End Sub
Private Sub OptionButton2_Click()
EnableOptBtns False
End Sub
Private Sub OptionButton3_Click()
EnableOptBtns True
End Sub
Private Sub EnableOptBtns(enable As Boolean)
Dim ctl As Control
Dim i As Long
For i = 4 To 11
Me.Controls("OptionButton" & i).Enabled = enable
Next i
End Sub
Related
I need a userform to load the the checkbox values in their unchecked form. They open with the boxes unchecked but they don't seem to be initializing that way. Here is the checkbox code and the open button code I have.
Private Sub cbxAdditional_Click()
If Me.cbxAdditional.Value Then
Me.Width = 524
Me.Height = 238.5
lstDatabase.Width = 480
txbxName.Visible = True
txbxComments.Visible = True
Else
Me.Width = 279
Me.Height = 238.5
lstDatabase.Width = 240
txbxName.Visible = False
txbxComments.Visible = False
End If
End Sub
--open code
Sub openform()
Test.Show
cbxAdditional = False
End Sub
I clearly am writing this wrong, when I opne the user form it starts in the IF form, how do I get it to open in the unchecked form?
Here's a very simple userform:
When you enter a userform, the UserForm_Initialize sub is executed. This sub is NOT automatically created in the code with the userform, but you can create it yourself. Any initialization or set up code should be performed here.
In my example below, I'm showing that the resetting of the checkboxes is a separate sub. I do this as a habit, just in case I have to add a reset button or something.
Option Explicit
Private Sub ClearCheckboxes()
Me.CheckBox1.Value = False
End Sub
Private Sub ResetButton_Click()
ClearCheckboxes
End Sub
Private Sub UserForm_Initialize()
ClearCheckboxes
End Sub
I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub
I am currently trying work on a code that can loop through all the check boxes and uncheck a box if the box next to it is checked.
Currently, I have something written out, and I know it's no where near what I need it to be. I know how to do it individually by box:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = False
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Value = False
End If
End Sub
However, I have several hundred lines of check boxes that I'd have to write this for so if I could just loop through the check boxes it would be a life saver!So I started out with:
Dim i As Integer
i = 1
a = 2
If CheckBoxi.Value = True Then
CheckBoxa.Value = False
End If
If CheckBoxa.Value = True Then
CheckBoxi.Value = False
End If
i = i + 2
a = a + 2
End
However this doesn't seem to work and I have no idea where to go from here. Any help would be greatly appreciated!
I'm having an issue with the way a listbox behaves on an Excel form. Steps to reproduce the issue:
Create a user form with one listbox control
Use following code with this user form:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
When the form is first shown, I am able to navigate the list box normally, using arrow keys and page keys. However, after the double-click event is triggered, all keyboard navigation has no effect, including tabbing to other controls (if they're on the form). Clicking on the listbox does seem to work, and the focus outline is shown correctly, but there's something wrong with the way the focus is handled after the listbox is locked and then unlocked. What is going on?
Using Office 2013 32-bit edition.
I managed to replicate this issue, and setting the focus somewhere else before locking and unlocking the listbox worked for me:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.SetFocus 'or some other control.
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
Me.ListBox1.SetFocus
End Sub
2 solutions I tested that allows you to use Arrow Keys to navigate.
Given that there isn't a single click event handler, try call the Single Click Event after the DblClick (works all the time):
Private Sub ListBox1_Click()
Debug.Print "ListBox1_Click()"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
ListBox1_Click
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Setting the Cancel = False at the end of DblClick. (Sometimes does not work!)
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
Cancel = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Changing the zoom in the sheet that has the listbox worked for me.
Private Sub Worksheet_Activate()
Dim temp As Double
Application.ScreenUpdating = False
'Change worksheet zoom setting for the active window
temp = ActiveWindow.Zoom
ActiveWindow.Zoom = temp + 10
ActiveWindow.Zoom = temp
Application.ScreenUpdating = True
End Sub
I want to click a check box and have another linked check box be disabled in Excel.
When I uncheck that particular check box, then the disabled check box should become enabled.
I tried so many things but not able to find the solution. Can I do this without a script?
I have 76 check boxes to work upon. So is this possible without a script? TIA
With two ActiveX check boxes the code would be:
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
Else:
CheckBox2.Enabled = True
End If
End Sub
It's simple, but works.
If you want the code to check and disable clicking on the second box, use the below code. If you uncheck the initial box, it will also enable and uncheck the second one.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = True
CheckBox2.Enabled = False
Else
CheckBox2.Value = False
CheckBox2.Enabled = True
End If
End Sub
more directly
Sub CheckBox1_Click()
CheckBox2.Enabled = Not CheckBox2.Enabled
End Sub
I belive you could use "OptionButton's". You don't need code for them. But in that way you'll be able to check only one of 76. And what do you mean by "linked check box"?
Option Compare Database
Private Sub Ck1_Click()
If Ck1 = True Then
Ck2.Enabled = False
Ck3.Enabled = False
Else
Ck2.Enabled = True
Ck3.Enabled = True
End If
End Sub
Private Sub Ck2_Click()
If Ck2 = True Then
Ck1.Enabled = False
Ck3.Enabled = False
Else
Ck1.Enabled = True
Ck3.Enabled = True
End If
End Sub
Private Sub Ck3_Click()
If Ck3 = True Then
Ck1.Enabled = False
Ck2.Enabled = False
Else
Ck1.Enabled = True
Ck2.Enabled = True
End If
End Sub