To end MessageBox that is in a loop

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
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
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
50,864
Office Version
  1. 365
Platform
  1. Windows
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
 

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi, the code ran smoothly and the MsgBox only appeared once. Really appreciate your help!
 

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
50,864
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,746
Messages
5,573,984
Members
412,562
Latest member
woodportaj
Top