Inserting error message into code

Tcarey

New Member
Joined
May 19, 2011
Messages
35
I cannot figure out where to enter an error message box if code can't find the entered value. Any suggestions?


Private Sub CommandButton1_Click()
Dim myString As String
Dim FoundCell As Range

myString = Trim(userform1.TextBox1.Value)
If myString = vbNullString Then
Exit Sub
End If
With Sheets("System").Range("A:A")
Set FoundCell = .Find(what:=myString, after:=.Cells(1, 1), LookAt:=xlWhole)
FoundCell.EntireRow.Copy
End With

Sheets("Risk").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try:-
Code:
If myString = vbNullString Then
[COLOR=red][B]  msgbox "The box is empty!",vbokonly+vbexclamation,"Error!"[/B][/COLOR]
  Exit Sub
End If
 
Upvote 0
I may not have been clear. This actually works great when the box is empty. However, I'm trying to add the MsgBox to anything other than a correct six digit code like 111211.
 
Last edited:
Upvote 0
Rich (BB code):
Private Sub CommandButton1_Click()

Dim myString As String
Dim FoundCell As Range

myString = Trim(UserForm1.TextBox1.Value)
If myString = vbNullString Then
Exit Sub
End If
On Error GoTo errHandler
With Sheets("System").Range("A:A")
Set FoundCell = .Find(what:=myString, LookIn:=xlValues, Lookat:=xlWhole)
FoundCell.EntireRow.Copy
End With

Sheets("Risk").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
Exit Sub
errHandler: MsgBox "Value not found"
End Sub
 
Upvote 0
Try...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Private[/color] [color=darkblue]Sub[/color] CommandButton1_Click()

    [color=darkblue]Dim[/color] myString [color=darkblue]As[/color] String
    [color=darkblue]Dim[/color] FoundCell [color=darkblue]As[/color] Range
    
    myString = Trim(Me.TextBox1.Value)
    
    [color=darkblue]If[/color] myString = vbNullString [color=darkblue]Then[/color]
        MsgBox "Please enter a value/search term...", vbExclamation
        Me.TextBox1.SetFocus
        [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
    [color=darkblue]With[/color] Sheets("System").Range("A:A")
        [color=darkblue]Set[/color] FoundCell = .Find(what:=myString, LookIn:=xlValues, LookAt:=xlWhole)
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]If[/color] [color=darkblue]Not[/color] FoundCell [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
        FoundCell.EntireRow.Copy
        Sheets("Risk").Cells(Sheets("Risk").Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
        Application.CutCopyMode = [color=darkblue]False[/color]
    [color=darkblue]Else[/color]
        MsgBox "Search string was not found...", vbExclamation
        [color=darkblue]With[/color] Me.TextBox1
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Value)
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

[/font]
 
Upvote 0
I tried Jim May's suggestoin and it is working perfectly for what I need.

I have another question, but need to find someone I can help before prodding along with my own issues. ;)

It appears you guys have it covered at the moment.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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