Compile Error on button click in VBA form - excel

I get
Compile Error: Expected function or variable
when I select a radio button and click ok on my form. The top line of code where the macro title is gets highlighted in yellow.
My form is four radio buttons and an ok button.
I've checked the macro names and option button names
Private Sub CommandButton1_Click()
If Ties.Radio_OpPlan.Value = True Then
Run clearties
ElseIf Ties.Radio_Prior.Value = True Then
Run TieToPrevious
ElseIf Ties.Radio_Custom.Value = True Then
Run CustomTie
ElseIf Ties.Radio_NetCase.Value = True Then
Run NetCaseTie
Else
output = MsgBox("You need to pick a case to tie to", vbExclamation)
End If
Me.Hide
output = MsgBox("Case tied out", vbOKOnly)
End Sub

Your issue is more than like due to the use of Run. This is used to call a macro from its name as a String.
Try removing Run from your different methods and see if that fixes it.
Private Sub CommandButton1_Click()
If Ties.Radio_OpPlan.Value = True Then
clearties
ElseIf Ties.Radio_Prior.Value = True Then
TieToPrevious
ElseIf Ties.Radio_Custom.Value = True Then
CustomTie
ElseIf Ties.Radio_NetCase.Value = True Then
NetCaseTie
Else
output = MsgBox("You need to pick a case to tie to", vbExclamation)
End If
Me.Hide
output = MsgBox("Case tied out", vbOKOnly)
End Sub
Additional Notes
Try not to use underscore case. Underscores have special meanings in VBA for events and implementations. So instead of Radio_OpPlan you could do RadioOpPlan, or even better simply OpPlan (really even make OpPlan even more descriptive).
In my opinion, a Case statement looks cleaner in this situation.
Select Case True
Case RadioOpPlan
clearties
Case RadioPrior
TieToPrevious
Case RadioCustom
CustomTie
Case RadioNetCase
NetCaseTie
Case Else
output = MsgBox("You need to pick a case to tie to", vbExclamation)
End Select

this is what worked, and all the macros had to make sure they started with selecting the sheet I was making changes on first
Private Sub CommandButton1_Click()
If Ties.OpPlan.Value = True Then
clearties
ElseIf Ties.Prior.Value = True Then
TieToPrevious
ElseIf Ties.Custom.Value = True Then
CustomTie
ElseIf Ties.NetCase.Value = True Then
NetCaseTie
Else
output = MsgBox("You need to pick a case to tie to", vbOKOnly)
End If
Me.Hide
End Sub

Related

Either of the checkbox can only be true

Situation
I have two form control check boxes. I am trying to write a code that will allow only either of them to be true.
my code is
Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox2.Enabled = False
Else
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
End If
End If
End Sub
I have this code in module and have assigned the same macro for both the checkboxes. I get run-time error 424. I beleive this is very basic problem but I unable to dela with it.
Thank you
Please, test the following way. Form check boxes do not have a click event, as ActiveX ones have. You should associate the next sub to both of them. The check boxes I tested, have their names as "Check Box 1" and "Check Box 2". You have to change yours according to the reality in your case, Please, copy the next code in a standard module and then associate it to both used check boxes:
Option Explicit
Sub FormCheckBoxChange()
If ActiveSheet.CheckBoxes(Application.Caller).value = 1 Then
Select Case Application.Caller
Case "Check Box 1": ActiveSheet.CheckBoxes("Check Box 2").value = -4146
Case "Check Box 2": ActiveSheet.CheckBoxes("Check Box 1").value = -4146
End Select
End If
End Sub
Use instead of the used check box names, the appropriate ones for your case.
In case of Form text boxes, their value is not True and False as in case of ActiveX ones. It is 1 and -4146...
Are you sure you want to enable/disable the checkboxes.
Following code makes sure that either one of both boxes is checked.
Public Sub checkbox2_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb2.Value = 1 Then oCb1.Value = 0
End Sub
Public Sub checkbox1_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb1.Value = 1 Then oCb2.Value = 0
End Sub

Updating checkbox value within CheckBox1_Click event runs code twice

I want the checkbox stays the same and generates a message box.
Private Sub CheckBox1_Click()
Dim beast As String
beast = Range("k1").Value
If beast = "No" Then
CheckBox1 = False
MsgBox "You Are Missing Ingredients", vbCritical, "Missing Ingredients"
End If
End Sub
Disable application events. Application events for ActiveX controls are trapped by the code sheet belonging to the worksheet on which the control has been installed. Therefore the code below must be on that code sheet, not a standard code module where it will never run.
Private Sub CheckBox1_Click()
Dim beast As String
With CheckBox1
If .Value = True Then
beast = Range("K1").Value
If beast = "No" Then
MsgBox "You Are Missing Ingredients", vbCritical, "Missing Ingredients"
' prevent the upcoming change from triggering this event
Application.EnableEvents = False
.Value = False
Application.EnableEvents = True
End If
End If
End With
End Sub
Make sure that your check box is an ActiveX control. In fact, once you create such a control it will be listed in the controls drop-down on the top left of the sheet's code module and VBA will create or select the click event procedure for you.
I have tested the above code and it doesn't run more thna once on one click. I have added a check to let it do nothing if the checkmark was removed rather than set.
You can use a static boolean variable to avoid the double execution, like so:
Private Sub CheckBox1_Click()
Static isBusy As Boolean
Dim beast As String
If Not isBusy Then
isBusy = True
beast = Range("k1").Value
If beast = "No" Then
CheckBox1.Value = False
MsgBox "You Are Missing Ingredients", vbCritical, "Missing Ingredients"
End If
isBusy = False
End If
End Sub
Static variables maintain their values across invocations, so if you set it in the first CheckBox1_Click invocation, you can check it in the second one and know a first invocation is ongoing.

Autofill cells based on drop down list in excel

I am trying to creata a VBA that gives me automatic values based on drop down list in a form. The problem is that when I run the macro then it is causing an error and excel stops working. Any help in this case is most welcome.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
First thing first, in your code, no need to put an Exit Sub before the End Sub.
The code will end after that line so this is a redundancy.
The next thing that you need to understand is that the Change Event will keep triggering if you will not disable it explicitly. So it means that when you hide a row on that Sheet, the Change Event will keep on happening since there will be changes that will happen on the Sheet. i.e. (Hiding Rows).
To do that you need to disable the EventsListeners of the application using the Application.EnableEvents = False. So the application can do a single thing based on that first event.
The next thing that you need to keep in mind is to track where the Changes occur and fire your program. Target is a Range Object that will return the Range where the specific change occurs on the Sheet.
In order to do that, you need to validate if you need to trigger the routine based on the target using the Intersect function.
The whole code is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("G11")) Is Nothing Then
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
End If
If Not Intersect(Target, Range("G12")) Is Nothing Then
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
End If
Application.EnableEvents = True
End Sub

Only allowing one checkbox to be checked for a userform

So I'm making a userform and I have to use make a group of mutually exclusive checkboxes or only allow the user to pick one. "But just use option buttons!" you cry. Two problems with that:
I already have a separate set of option buttons in the userform (I believe you can somehow group them to allow multiple sets but I am unfamiliar with how to actually do this).
My professor specifically wants checkboxes
so I attempted to solve this problem like this
If CheckBoxBar.Value = True And CheckBoxatm.Value = True Then
GoTo Here:
End If
If CheckBoxatm.Value = True And CheckBoxmmHg.Value = True Then
GoTo Here:
End If
If CheckBoxatm.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
If CheckBoxBar.Value = True And CheckBoxmmHg.Value = True Then
GoTo Here:
End If
If CheckBoxBar.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
If CheckBoxmmHg.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
The here leads to a message box that re initializes the userform after the msg box says "You are only allowed to select one" with code like this
Here: MsgBox "You are only allowed to select on pressure unit."
The code "works" but it always goes to the Here: statement despite only picking one of the checkboxes. Can you spot anything wrong?
Thanks for the help!
As stated by Doug Glancy in a comment, your current code is probably missing an Exit Sub before the label Here:, which is therefore allowing your code to fall into the section after the label.
Another way of achieving what you are after is just to have one If statement which checks if more than one CheckBox is checked, and then display the MsgBox if so, e.g. as follows:
If CheckBoxBar.Value + _
CheckBoxatm.Value + _
CheckBoxmmHg.Value + _
CheckBoxpsia.Value < -1 Then
MsgBox "You are only allowed to select one pressure unit."
Exit Sub
End If
Or you can rely on the .Value being the default property of a CheckBox and thus "reduce" that code to:
If CheckBoxBar + CheckBoxatm + CheckBoxmmHg + CheckBoxpsia < -1 Then
MsgBox "You are only allowed to select one pressure unit."
Exit Sub
End If
Note: This method won't work if the .TripleState property of the CheckBox is set to True. (My thanks to Comintern for pointing that out.)
You can make the checkboxes act like option buttons if you override the click function and clear the other boxes (although you can also have none selected).
Private Sub CheckBoxatm_Click()
If Me.Controls("CheckBoxatm").Value = True Then Call ClearOtherValues("CheckBoxatm")
End Sub
Private Sub CheckBoxBar_Click()
If Me.Controls("CheckBoxBar").Value = True Then Call ClearOtherValues("CheckBoxBar")
End Sub
Private Sub CheckBoxmmHg_Click()
If Me.Controls("CheckBoxmmHg").Value = True Then Call ClearOtherValues("CheckBoxmmHg")
End Sub
Private Sub CheckBoxpsia_Click()
If Me.Controls("CheckBoxpsia").Value = True Then Call ClearOtherValues("CheckBoxpsia")
End Sub
Private Function ClearOtherValues(cb As String)
Dim cbPressure() As String, i As Long
cbPressure = Split("CheckBoxBar,CheckBoxatm,CheckBoxmmHg,CheckBoxpsia", ",")
For i = 0 To UBound(cbPressure)
If cbPressure(i) <> cb Then Me.Controls(cbPressure(i)).Value = False
Next i
End Function

OptionButton determines value then use in macro

A basic simple VBA question that I havent been able to work out even though there are numerous tutorials about it.
I want a Userform to pop-up, give you two options (OptionButton1 and OptionButton2). You choose one, click ok. And depending on what Option is chosen a certain value is used (in an email, for which I DID finish the macro). For simplifying purposes i now just want the variable 'contents' to be printed in cell A1.
I have these parts so far:
Sub MailOption()
UserForm1.Show
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then Var1 = "Text example 1"
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then Var1 = "Text example 2"
End Sub
Private Sub SendEmail_Click()
Cells(1, 1).Value = Var1
End Sub
There are multiple problems: the variable is not shown in Cell A1 and when i press Send Email the Form is not closed. I am probably doing lots of stuff wrong but its the first time im using a userform. Ty very much
I would simply use this one without handling OptionButton_Click:
Private Sub SendEmail_Click()
Dim res As String
If OptionButton1.Value Then
res = "Text example 1"
ElseIf OptionButton2.Value Then
res = "Text example 2"
Else
res = "Nothing is selected"
End If
'write result in cell
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = res
'close form
Unload Me
End Sub

Resources