How do I end my macro when i press cancel?

dianeslao

New Member
Joined
Mar 24, 2018
Messages
4
Hi,
Below is my macro, basically inside Do Loop are a set of macro that opens a file, copy a sheet to another excel file.
all i want is if i dont have a file to upload to my new excel, i just want to return to my main file without being prompted by the Run Time error pop up.
please help

Sub Main()


Do
Addsheet
Compile
Copy
InsertDate
Header
Loop




End Sub
 

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
Hi Diane,

please use code tags around your code to make it more legible (see example in red below)

Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer


Sub Main()
    Dim Result As Boolean
    Dim x As Integer
    
    
    Do
        For x = 27 To 27 'Esc
            Result = GetAsyncKeyState(x)
            If Result Then
                Exit Sub
            End If
        Next
        Addsheet
        Compile
        Copy
        InsertDate
        Header
    Loop
End Sub
 
Upvote 0
Maybe I misunderstood. The above code will exit when you press the Escape key somewhere in the process. But now I read you want to press cancel?? Is this on a userform?
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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