message box with option button inside sheet doesn't work

Hasson

Active Member
Joined
Apr 8, 2021
Messages
403
Office Version
  1. 2016
Platform
  1. Windows
Hello
I have to optionbutton 1,2 inside the sheet(not on userform)
and I have code in commandbutton 1
if I put this procedure before my code
VBA Code:
Private Sub CommandButton1_Click()
If Sheets("DATA").Range("G1") <> "PURCHASE" Or Sheets("DATA").Range("G1") = "SALES" Then

MsgBox "please select one of  the optionbutton ", vbCritical
Exit Sub
End If
'rest of  the code
'
'
and the option button1,2
VBA Code:
Private Sub OptionButton1_Click()
'code
'
'
If OptionButton1.Value = True Then Sheets("DATA").Range("G1") = "SALES"

End Sub

Private Sub OptionButton2_Click()
'code
'
'

If OptionButton2.Value = True Then Sheets("DATA").Range("G1") = "PURCHASE"

End Sub
so if two option buttons are false,then will show message box when click commandbutton1 , and if one of the option button is true then should run the code without show message , but it always will show the message , how I can fix it?
so if the G1=PURCHASE OR SALES based on optionbutton1 or 2 then should run the code in commandbutton1 .
if the G1= empty or equal different word then should show message.
thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Select Case Sheets("DATA").Range("G1").Value
    Case "PURCHASE"
      'do some
    Case "SALES"
      'do some
    Case Else
      MsgBox "please select one of  the optionbutton ", vbCritical
      Exit Sub
  End Select
End Sub

Or this:
Rich (BB code):
Private Sub CommandButton1_Click()
  With Sheets("DATA")
    If .Range("G1").Value <> "PURCHASE" And .Range("G1").Value <> "SALES" Then
      MsgBox "please select one of  the optionbutton ", vbCritical
      Exit Sub
    End If
  End With
  '
  'code...
  '
End Sub
 
Upvote 1
Solution
great !

I see the second option is better because will deal two cases together , but when use SELECT CASE instead of IF ,then the code will deal every case individuall (mean I have to repeat the same code under each case if I'm not wrong .

thanks for you pay my attention by bold part of the line to understand what my mistake.;)
now every thing is ok .(y)
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,420
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top