OK Command button on User Form

funkykayli

Board Regular
Joined
Apr 25, 2007
Messages
183
I have a user form with an OK button on it. I have the following code attached to the OK button:

Private Sub cmdOK_Click()
If Me.cboTreat.Value = "" Then
MsgBox "Please Choose a Treatment Type.", vbExclamation, "Welcome"
Me.cboTreat.SetFocus
End If
End Sub

The code works fine. If someone clicks on the OK button without choosing an item from the combo box the message box appears. What I want to do is if the user chooses an item from the combo box and then clicks ok I want the focus to go to the workbook sheet Pivot. I tried entering an else statement and putting in sheet("Pivot").activate but this does not seem to work. Am I on the right track entering an esle statement?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Doesn't this work?

Code:
Private Sub cmdOK_Click()
  If Me.cboTreat.Value = "" Then
    MsgBox "Please Choose a Treatment Type.", vbExclamation, "Welcome"
    Me.cboTreat.SetFocus
  Else
    Sheets("Pivot").Activate
  End If
End Sub
 
Upvote 0
You know what...it does work...thank you. I guess the problem is that the user form stays on top of the sheet. How do I turn the focus to the excel workbook?
 
Upvote 0
I do have this code in my ThisWorkbook

Option Explicit
Private Sub Workbook_Open()
Worksheets("Pivot").Activate
frmWelcome.Show
End Sub
 
Upvote 0
You could close the userform:

Rich (BB code):
Private Sub Workbook_Open()
  Worksheets("Pivot").Activate
  frmWelcome.Show
  Unload frmWelcome
End Sub
 
Upvote 0
Try
Code:
Private Sub cmdOK_Click()
  If Me.cboTreat.Value = "" Then
    MsgBox "Please Choose a Treatment Type.", vbExclamation, "Welcome"
    Me.cboTreat.SetFocus
  Else
    Sheets("Pivot").Activate
    Unload Me
  End If
End Sub
 
Upvote 0
Yeah, the form didn't hide itself, so code execution did not return to the procedure that opened the form. So Mike bailed me out again.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
rootytrip

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