Monday, August 6, 2018

Excel VBA User Form option buttons


I've created a userform that will require an option button to be selected (thus emailing the intended recipient), otherwise it will display a msgbox.


I'm having a problem with the If And Then Else statements. I need the code to continue if one of the option buttons is selected, only display the msgbox if there are no option buttons selected.


I'm sure this is a newbie error. Any help would be greatly appreciated.



Private Sub cmdSend_Click()
If optbeasley.Value = False _
And optmaney.Value = False _
And optmessana.Value = False _
And opttimmerman.Value = False _
And opttrotter.Value = False _
Then MsgBox "Please Select a Contact"
Else:
Next i
Call Important_relocate_reformat
If Me.optbeasley.Value = True Then
Call Module4.Email_beasley
Unload Me
End If
If Me.optmaney.Value = True Then
Call Module4.Email_maney
Unload Me
End If
If Me.optmessana.Value = True Then
Call Module4.Email_messana
Unload Me
End If
If Me.opttimmerman.Value = True Then
Call Module4.Email_timmerman
Unload Me
End If
If Me.opttrotter.Value = True Then
Call Module4.Email_trotter
Unload Me
End If
If Me.chkMattBeasley.Value = True Then
Call Email_beasley
End If
If Me.chkRickLeshane.Value = True Then
Call Email_Important_Leshane
End If
If Me.chkTimRuppert.Value = True Then
Call Email_Important_Ruppert
End If
End Sub
Private Sub optbeasley_Click()
If optbeasley.Value = True Then
chkMattBeasley.Enabled = False
Else
chkMattBeasley.Enabled = True
End If
End Sub
Private Sub optmaney_Click()
Call optbeasley_Click
End Sub
Private Sub opttimmerman_Click()
Call optbeasley_Click
End Sub
Private Sub opttrotter_Click()
Call optbeasley_Click
End Sub
Private Sub optmessana_Click()
Call optbeasley_Click
End Sub

I've edited to show the usage of your code:


Private Sub cmdSend_Click()
If optbeasley = 0 And optmaney = 0 And optmessana = 0 And opttimmerman = 0 And opttrotter = 0 Then
MsgBox "Please Select a Contact"
Else: Call cmdSend_Click2
End If
End Sub

Answer



It's the formatting, apparently. This works -


Sub example()
Dim optbeasley As Range
Dim optmaney As Range
Dim optmessana As Range
Dim opttimmerman As Range
Dim optrotter As Range
Set optbeasley = Range("A1")
Set optmaney = Range("A2")
Set optmessana = Range("A3")
Set opttimmerman = Range("A4")
Set optrotter = Range("A5")
If optbeasley = False _
And optmaney = False _
And optmessana = False _
And opttimmerman = False _
And opttrotter = False Then
MsgBox "Please Select a Contact"
Else: MsgBox ("hi")
End If
End Sub

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...