I’m writing a simple macro and am a bit rusty in my VBA skills. The macro has a list of options with “yes”, “no” and “maybe” options to choose from. I used checkboxes rather than option buttons so each option doesn’t have to be in an individual frame.
The code I have written (below) deselects the other options when one is selected, but I have to select it again for it to actually be selected which will be frustrating for the user.
Private Sub Yes_Click()
No.Value = False
Maybe.Value = False
End Sub
I have tried adding the additional line
Yes.Value = True
But this then crashes excel when the macro is run.
I can’t find any advice for this problem so any help is greatly appreciated.
Related
Is it possible to save changes to a Label Caption in a userform in Excel VBA, so that they are permanently saved, and only changed when you enter a new change?
I have checked, that the code is changing the caption, but I cannot get it to stick, so that it is still there next time I open the userform.
Thank you in advance
Private Sub cmdSubmit_Click()
'resets participants email and name
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
Check out this userform example and you'll find answers to many of your questions.
Source of userform
Forgive the lengthy answer initially, but there is a bit of a risk involved with making permanent changes to UserForms through VBA.
In order to change the caption of a Label (or UserForm or any other Control permanently, you will have to "Trust access to the VBA project object model" in order to do it via VBA code. Now, while this is possible it is not usually recommended because it can seriously put a user's PC at Risk should they encounter a Macro developed for nefarious purposes.
(To clarify in case the question is raised, your end user(s) will have to make this Trust setting change on their PC's as well . . . you cannot make the change on your PC, setup the code to work and then hand the file over to another user and have it work on their PC without them making the same change.)
There are methods to do this programmatically, but, this falls into the "nefarious Macro" rabbit hole and would need to be disclosed to the end user you are making this change. . . Research at your own risk.
If you are OK with putting yourself at risk, you can do it using VBA similar to the following snippet I found here. You will have to substitute your UserForm name and Label name as appropriate. I tested it on my own UserForm and it works as expected.
Sub Change_Userform()
ThisDocument.VBProject.VBComponents("Userform1").Designer.Controls("Label1").Caption = "Some new caption text"
End Sub
You will need to do some research on how to "Trust access to the VBA project object model" yourself and understand the risks to do this in order for the above code to work.
If I understand the intent of what you are trying to accomplish correctly though, you could achieve this effect without having to put yourself or your end user(s) at risk.
(Most end user(s) typically would not have direct access to the VBA Designer where they would see the UserForm's un-initialized environment.) To do this, you would have to place your code in the UserForm's event.
The following assumes your Me.optProg.Value and Me.optTester.Value are Option Buttons which a user would change. If you create a "Settings" sheet in the file, you can place values in the cells of this sheet and then hide it so the users do not modify them directly. Then, reference the values of the cells and change the appearances of the Option Buttons at the same time as the UserForm's launch. (Additionally, you can set the Click events of the Option Buttons to change the values of the same cells and provide that change to affect the UserForm's Initialize event when called, but this should get you going in the right direction.)
Sub UserForm_Initialize()
'The Range below is completely up to you.
'Since you are using Boolean True/False, a simple "1" or "0" _
is easy to use to make the changes.
If Thisworkbook.Sheets("Some_Settings").Range("A1").Value = "1" Then
Me.optProg.Value = True
Else
Me.optTester.Value = True
End If
'do some other code here as needed to finish initializing the UserForm
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
I am trying to do an Excel project for work. It involves a lot of listboxes. I needed the listboxes to have a double click event where the single click selects and a double click deselects. After some research, I was able to find some code that would perform that function perfectly:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Application.EnableEvents = False
Me.ListBox1.ListIndex = -1
Application.EnableEvents = True
End Sub
However, there is so much code for all of the listboxes that I think it is making the file glitch. Excel has crashed several times. Each listbox just needs the double click event. Is there a way to combine all this into one general command? Please dumb down your responses, as I am not a programmer. I am in way over my head. Thanks!
I am working on a project that requires me to disable certain option buttons in excel 2010 based on the results of prior option buttons. Some criteria will need multiple if statements, similar to a nested if. Some info; cell B107 in my example reflects which of the 5 option buttons they might have chosen. I have tried the following code but I have not been able to find success. Additionally, how would I make the disabled option button appear grey.
Private Sub OptionButton22_Change()
If Range ("B108") = 1 Then
OptionButton22.Enabled = False
Else
OptionButton22.Enabled = True
End If
End Sub
I am maintaining a large spread sheet at work and there are ActiveX command buttons throughout the code. One sheet I am currently working on has almost 100 of these. I have made sure the button itself is visible as some of the buttons are hidden/unhidden depending on the flow of the code.
Is there a way I can find whereabouts on the sheet itself the button is located that the VBA code is pointing to? Here is a snippet:
enter code here
Private Sub CommandButton4_Click()
Application.Goto Range("Add_Trainees"), True
CommandButton3.Visible = True ' unhides menu button
CommandButton81.Visible = True ' hides SC1
CommandButton97.Visible = True ' hides SC2
End Sub
I am trying to find where on the sheet where command buttons 3, 81 and 97 are located.
TIA for any help/suggestions.
I think pnuts provides the correct way to find out where the buttons are.
I would also suggest to open the panel from "Developer" -> "Properties". In that panel, you can check out all the ActiveX Controls, including the command buttons, in the list.
I'm creating an Excel vba-based application in which specific data should be shown, and for filtering this information I'm trying to use ActiveX Chekboxes.
After I create the checkboxes I can check/uncheck them normally, and I use some macros to perform somethings I want, for example, to make sure only one option can be checked I use:
Private Sub CheckBox1_Click()
If Worksheets("Cover").CheckBox1.Value = True Then Range("O8") = 1
If Worksheets("Cover").CheckBox1.Value = False Then Range("O8") = 0
If Worksheets("Cover").Range("O8") = 1 Then
Worksheets("Cover").CheckBox2.Value = False
Worksheets("Cover").CheckBox3.Value = False
Worksheets("Cover").CheckBox4.Value = False
End If
End Sub
The problem is that, after I save this worksheet and reopen it to start working again, I can't check or uncheck the boxes anymore. I also can't format them on Designer Mode anymore (if I right-click one of the checkboxes and select 'Properties' a window to change the properties of the active worksheet appears). And, of course, the macros don't work anymore (Runtime error '438').
Does anyone have a clue about what may be the problem and how to solve it?
Thanks in advance!