To end MessageBox that is in a loop

jxj_00

New Member
Joined
Oct 1, 2020
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi

I have written a code that searches for a database for employee ID. However, the message box appears multiple time

I want my code to do the following:
1. If employee ID exists, copy and paste the necessary info
2. If employee ID is not found, a message box appears once 'No records found'

VBA Code:
Private Sub CommandButton3_Click()

Dim inputEmployeeID As String, EmployeeID As String
EmployeeID = Trim(TextBox1.Text)
Dim srcWS As Worksheet, desWS As Worksheet, lastrow As Long, i As Integer, erow As Long
Set srcWS = Workbooks("Master Database.xlsm").Worksheets(" Database")
Set desWS = Workbooks("Employee_WB.xlsm").Worksheets("Employee_WS")

'Find filled row in database
lastrow = srcWS.Cells(Rows.Count, 1).End(xlUp).Row

'Find first empty row in database
erow = desWS.Cells(Rows.Count, 2).End(xlUp).Row + 1

inputEmployeeID = TextBox1.Text
For i = 2 To lastrow
    If EmployeeID = srcWS.Cells(i, 1).Value Then
        srcWS.Cells(i, 1).Copy
        desWS.Cells(erow, 2).PasteSpecial Paste:=xlPasteValues
        srcWS.Cells(i, 2).Copy
        desWS.Cells(erow, 3).PasteSpecial Paste:=xlPasteValues
        srcWS.Cells(i, 3).Copy
        desWS.Cells(erow, 4).PasteSpecial Paste:=xlPasteValues
        srcWS.Cells(i, 4).Copy
        desWS.Cells(erow, 5).PasteSpecial Paste:=xlPasteValues
        Exit For
        erow = erow + 1
     Else
     MsgBox ("No record found")
     
    End If
Next i
TextBox1.Value = ""

UserForm1.Hide
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
VBA Code:
Private Sub CommandButton3_Click()

Dim EmployeeID As String
Dim Fnd As Range
EmployeeID = Trim(TextBox1.Text)
Dim srcWS As Worksheet, desWS As Worksheet, lastrow As Long, erow As Long
Set srcWS = Workbooks("Master Database.xlsm").Worksheets(" Database")
Set desWS = Workbooks("Employee_WB.xlsm").Worksheets("Employee_WS")

'Find filled row in database
lastrow = srcWS.Cells(Rows.Count, 1).End(xlUp).Row

'Find first empty row in database
erow = desWS.Cells(Rows.Count, 2).End(xlUp).Row + 1

Set Fnd = srcWS.Range("A:A").Find(EmployeeID, , , xlWhole, , , True, , False)
If Fnd Is Nothing Then
   MsgBox "No record found"
Else
   desWS.Range("B" & erow).Resize(, 4).Value = Fnd.Resize(, 4).Value
End If
TextBox1.Value = ""

UserForm1.Hide
End Sub
 
Upvote 0
Hi, the code ran smoothly and the MsgBox only appeared once. Really appreciate your help!
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton3_Click()

Dim EmployeeID As String
Dim Fnd As Range
EmployeeID = Trim(TextBox1.Text)
Dim srcWS As Worksheet, desWS As Worksheet, lastrow As Long, erow As Long
Set srcWS = Workbooks("Master Database.xlsm").Worksheets(" Database")
Set desWS = Workbooks("Employee_WB.xlsm").Worksheets("Employee_WS")

'Find filled row in database
lastrow = srcWS.Cells(Rows.Count, 1).End(xlUp).Row

'Find first empty row in database
erow = desWS.Cells(Rows.Count, 2).End(xlUp).Row + 1

Set Fnd = srcWS.Range("A:A").Find(EmployeeID, , , xlWhole, , , True, , False)
If Fnd Is Nothing Then
   MsgBox "No record found"
Else
   desWS.Range("B" & erow).Resize(, 4).Value = Fnd.Resize(, 4).Value
End If
TextBox1.Value = ""

UserForm1.Hide
End Sub

Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,559
Messages
6,114,302
Members
448,564
Latest member
ED38

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