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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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"
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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