"Cancel" Button Doesn't Work on Dialogue Box

bobsburgers

Board Regular
Joined
Jun 25, 2017
Messages
60
Hi, All!

Whenever I hit press the "Cancel" button on the dialogue box, the worksheet still seems to run the function while searching for nothing.

I'm not sure if it's an issue with the working in VBA, or an issue with the dialogue box, but I haven't been able to find a solution.

Below is the code:

Code:
Sub Item_Return()


    Dim scanstring As String
    Dim foundscan As Range
    Dim ws As Worksheet
    Dim foundscan_address As String
    
Set ws = ActiveSheet


scanstring = InputBox("Please enter a value to search for", "Enter value")


With ws.Columns("D")
    
    Set foundscan = .Find(What:=scanstring, LookIn:=xlValues, LookAt:=xlWhole, _
                          SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                          MatchCase:=False, SearchFormat:=False)
                      
    If Not foundscan Is Nothing Then
foundscan_address = foundscan.Address

Do
        foundscan.Offset(0, 4).Value = scanstring
        ws.Activate
        foundscan.Activate
        ActiveWindow.ScrollRow = foundscan.Row

        Set foundscan = .FindNext(foundscan)

Loop While Not foundscan Is Nothing And foundscan.Address <> foundscan_address

    Else
        MsgBox scanstring & "  was not found"
    End If
    
End With

End Sub

Please let me know if I can provide you with any more information - thank you!!

Best,

Bob
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The code is just following orders, as there's nothing there to stop it. ;)

Try adding the red bit below:

Rich (BB code):
scanstring = InputBox("Please enter a value to search for", "Enter value")
If scanstring = "" then Exit Sub
 
Upvote 0
Hi, RoryA!

Thank you so much - this seems to work perfectly! I'm still learning the VBA language, and sometimes something as simple as a stop command evades me!

Best,

Bob
 
Last edited:
Upvote 0
Hi, again, Rory A -

I have another question regarding this code on anther thread (link below). If you have a second, would you mind taking a look at it? I haven't been able to get any responses regarding this issue.

How to make a Macro "On/Off"

Let me know - thanks again!!

Best,

Bob:cool:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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