Activate next

Keith.Jackson

Board Regular
Joined
Mar 31, 2005
Messages
115
I have an application that uses the following code:

Application.ActiveWindow.ActivateNext
Range("A1:Z500").Select
Selection.Copy
Application.ActiveWindow.ActivatePrevious
ActiveSheet.Paste

When there is no other workbook open, the current workbook copies from itself.

How can I make the code show an error and end the procedure when there is no other open workbook?

Thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Use the Application.Workbooks.Count property as in:
Code:
If Application.Workbooks.Count = 2 Then
    Application.ActiveWindow.ActivateNext 
    Range("A1:Z500").Copy 
    Application.ActiveWindow.ActivatePrevious 
    ActiveSheet.Paste 
ElseIf Application.Workbooks.Count = 1 Then
    MsgBox "...you only have 1 workbook open!",vbCritical, "Operation cancelled..."
    Exit Sub
Else
    MsgBox "...you have at least 2 workbooks open!  Your use of .ActivateNext and ActivatePrevious could be potentially catastrophic!",vbCritical, "Operation cancelled..."
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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