Adding Error with Copy & Paste

dan8825

New Member
Joined
Dec 24, 2018
Messages
26
Hello I have the below code, which should show an error msg box and close the two workbooks the macro has already opened when there is nothing to paste.

This is working to well! as in it runs even when there is copied text.

Code:
  Sheets("Tab 2").Select    Range("A1").Select
      ActiveSheet.PasteOn Error GoTo ErrorHandler
ErrorHandler:
    Mainbook.Close Savechanges:=False
    wb1.Close Savechanges:=False
    MsgBox "Nothing to paste!": Err.Clear
    Exit Sub
    Range("A1:F500").Select
    Selection.Style = "Normal"

I need code to run an error message,end sub, close the workbooks it opened when there is noting to paste, otherwise run as normal.

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,056
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
If this is your code as typed (i.e. you haven't chopped out any lines from the middle) then your problem is because you have the error code directly after the paste statement, so it will always be run. Insert an Exit Sub (shown in red) so it will be:
Code:
  Sheets("Tab 2").Select    Range("A1").Select
      ActiveSheet.PasteOn Error GoTo ErrorHandler
[COLOR=#ff0000]    Exit Sub[/COLOR]
ErrorHandler:
    Mainbook.Close Savechanges:=False
    wb1.Close Savechanges:=False
    MsgBox "Nothing to paste!": Err.Clear
    Exit Sub
    Range("A1:F500").Select
    Selection.Style = "Normal"
 

dan8825

New Member
Joined
Dec 24, 2018
Messages
26
Hi This is now the full code, the only bit proceeding this is opening the workbooks. This is not working it is pasting 'Activesheet.paste' into a workbook.

The copy would be done manually before hand, as this is in an external programme not another workbook

Code:
    Sheets("Tab 2").Select    Range("A1").Select
   ActiveSheet.Paste
   On Error GoTo ErrorHandler
    Exit Sub


ErrorHandler:
    Mainbook.Close Savechanges:=False
    wb1.Close Savechanges:=False
    MsgBox "Nothing to paste!": Err.Clear
    Exit Sub
    Range("A1:F500").Select
    Selection.Style = "Normal"

Thanks
 

dan8825

New Member
Joined
Dec 24, 2018
Messages
26
If this is your code as typed (i.e. you haven't chopped out any lines from the middle) then your problem is because you have the error code directly after the paste statement, so it will always be run. Insert an Exit Sub (shown in red) so it will be:
Code:
  Sheets("Tab 2").Select    Range("A1").Select
      ActiveSheet.PasteOn Error GoTo ErrorHandler
[COLOR=#ff0000]    Exit Sub[/COLOR]
ErrorHandler:
    Mainbook.Close Savechanges:=False
    wb1.Close Savechanges:=False
    MsgBox "Nothing to paste!": Err.Clear
    Exit Sub
    Range("A1:F500").Select
    Selection.Style = "Normal"

See post above, thanks
 

dan8825

New Member
Joined
Dec 24, 2018
Messages
26

Forum statistics

Threads
1,148,108
Messages
5,744,875
Members
423,907
Latest member
zerocool88

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
Top