Message Box appears too much even if its not supposed to be show up

Sekaya

New Member
Joined
Apr 17, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone. Im just started practice on making macros. That macro is about finding a value from table 1 from sheet 1 copy and paste to table 2 from sheet 2 and delete it from table 1. It looks like working well but then i tried to add message box for unmatched value. If there is no match with value message box appears 4-5 times. Even if there are match macro doing the job but message box still appears 3-4 times. How can i fix it ?

VBA Code:
Sub CopyProcedure()

Dim tdate As Date
tdate = Date
Dim x As String
x = InputBox("Tekne Adını Giriniz", "WİM")

    Dim i As Long
    Dim lRow1 As Long, lRow2 As Long
    Dim wsSayfa1 As Worksheet, wsSayfa2 As Worksheet

    Set wsSayfa1 = Sheets("Sayfa1")
    Set wsSayfa2 = Sheets("Sayfa2")

    lRow1 = wsSayfa1.Range("B" & wsSayfa1.Rows.Count).End(xlUp).Row

    
    For i = 2 To lRow1
        
        If wsSayfa1.Range("B" & i).Text = x Then
            
            lRow2 = wsSayfa2.Range("A" & wsSayfa2.Rows.Count).End(xlUp).Row + 1
            
            wsSayfa1.Range("B" & i, "D" & i).Copy
            
            wsSayfa2.Range("B" & lRow2).PasteSpecial xlPasteValues
            
            wsSayfa2.Range("E" & lRow2) = tdate
            wsSayfa2.Range("G" & lRow2) = ("VAR")
            Application.CutCopyMode = False
            wsSayfa1.Range("B" & i).EntireRow.Delete
        Else
            MsgBox "Girdiğiniz Tekne Adı Bulunamadı.", vbOKOnly, "WİM"
            
        End If
                   
    Next i

    Set wsSayfa1 = Nothing
    Set wsSayfa2 = Nothing

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi & welcome to MrExcel.
Why not check to see if the value exists before the loop like
VBA Code:
Sub CopyProcedure()

Dim tdate As Date
tdate = Date
Dim x As String
x = InputBox("Tekne Adini Giriniz", "WIM")

    Dim i As Long
    Dim lRow1 As Long, lRow2 As Long
    Dim wsSayfa1 As Worksheet, wsSayfa2 As Worksheet
    Dim Found As Range
    
    Set wsSayfa1 = Sheets("Sayfa1")
    Set wsSayfa2 = Sheets("Sayfa2")

    Set Found = wsSayfa1.Range("B:B").Find(x, , , xlWhole, , , True, , False)
    If Found Is Nothing Then
         MsgBox "Girdiginiz Tekne Adi Bulunamadi.", vbOKOnly, "WIM"
         Exit Sub
    End If
    lRow1 = wsSayfa1.Range("B" & wsSayfa1.Rows.Count).End(xlUp).Row

    
    For i = 2 To lRow1
        
        If wsSayfa1.Range("B" & i).Text = x Then
            
            lRow2 = wsSayfa2.Range("A" & wsSayfa2.Rows.Count).End(xlUp).Row + 1
            
            wsSayfa1.Range("B" & i, "D" & i).Copy
            
            wsSayfa2.Range("B" & lRow2).PasteSpecial xlPasteValues
            
            wsSayfa2.Range("E" & lRow2) = tdate
            wsSayfa2.Range("G" & lRow2) = ("VAR")
            Application.CutCopyMode = False
            wsSayfa1.Range("B" & i).EntireRow.Delete
        End If
                   
    Next i

    Set wsSayfa1 = Nothing
    Set wsSayfa2 = Nothing

End Sub
 
Upvote 0
Solution
Hi & welcome to MrExcel.
Why not check to see if the value exists before the loop like
VBA Code:
Sub CopyProcedure()

Dim tdate As Date
tdate = Date
Dim x As String
x = InputBox("Tekne Adini Giriniz", "WIM")

    Dim i As Long
    Dim lRow1 As Long, lRow2 As Long
    Dim wsSayfa1 As Worksheet, wsSayfa2 As Worksheet
    Dim Found As Range
   
    Set wsSayfa1 = Sheets("Sayfa1")
    Set wsSayfa2 = Sheets("Sayfa2")

    Set Found = wsSayfa1.Range("B:B").Find(x, , , xlWhole, , , True, , False)
    If Found Is Nothing Then
         MsgBox "Girdiginiz Tekne Adi Bulunamadi.", vbOKOnly, "WIM"
         Exit Sub
    End If
    lRow1 = wsSayfa1.Range("B" & wsSayfa1.Rows.Count).End(xlUp).Row

   
    For i = 2 To lRow1
       
        If wsSayfa1.Range("B" & i).Text = x Then
           
            lRow2 = wsSayfa2.Range("A" & wsSayfa2.Rows.Count).End(xlUp).Row + 1
           
            wsSayfa1.Range("B" & i, "D" & i).Copy
           
            wsSayfa2.Range("B" & lRow2).PasteSpecial xlPasteValues
           
            wsSayfa2.Range("E" & lRow2) = tdate
            wsSayfa2.Range("G" & lRow2) = ("VAR")
            Application.CutCopyMode = False
            wsSayfa1.Range("B" & i).EntireRow.Delete
        End If
                  
    Next i

    Set wsSayfa1 = Nothing
    Set wsSayfa2 = Nothing

End Sub
Thank you for your answer. It worked very well.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,789
Members
449,126
Latest member
Greeshma Ravi

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