VBA - Search Table for Match

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Hello!

Edit: Apologies, incorrect posting title :(

I created an IF statement to display a MsgBox if both a Combo Box and Text Box were filled. The MsgBox comes up with no issues, however once you click "Ok" on the MsgBox, instead of taking you back to the Userform like I want it to, it cycles through the rest of the code and opens up the next form. Is there a way that after clicking Ok on the MsgBox, it just takes you back to the same form so you can edit the Userform?

VBA Code:
Private Sub CommandButton1_Click()

'Check to see if Valid search entries
If ComboBox1.Value <> "" And Trim(TextBox1.Text) <> "" Then
    MsgBox "Please use the drop down menu OR the Last Name text box. You cannot search using both."
ElseIf ComboBox1.Value = "" And Trim(TextBox1.Text) = "" Then
    MsgBox "Invalid parameters."
Else

'Write data to cell for lookup later
If ComboBox1.Value <> "" Then
    Range("FullNameLookUp").Value = ComboBox1.Value
Else
    Range("LastNameLookUp").Value = TextBox1.Value
End If

End If

Unload Me
With EmployeeLookup
  .StartUpPosition = 0
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about this for a "UserForm1"

VBA Code:
Private Sub CommandButton1_Click()

'Check to see if Valid search entries
If ComboBox1.Value <> "" And Trim(TextBox1.Text) <> "" Then
    MsgBox "Please use the drop down menu OR the Last Name text box. You cannot search using both."
    UserForm1.Show
    Exit Sub
ElseIf ComboBox1.Value = "" And Trim(TextBox1.Text) = "" Then
    MsgBox "Invalid parameters."
    UserForm1.Show
    Exit Sub
Else

'Write data to cell for lookup later
If ComboBox1.Value <> "" Then
    Range("FullNameLookUp").Value = ComboBox1.Value
Else
    Range("LastNameLookUp").Value = TextBox1.Value
End If

End If

Unload Me
With EmployeeLookup
  .StartUpPosition = 0
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With


End Sub
 
Upvote 0
Solution
You're welcome, I was happy to help. Thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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