Prevent Macro From Running In Other Workbooks

J Lee

New Member
Joined
May 21, 2008
Messages
34
I have a workbook Productivity.xls where users will enter data. The data fields are scattered and do not follow any sort of organized pattern and can not be moved. Everything except the fields they can access are locked. Because of this, when you press Enter or down in certain cells the screen will jump to the bottom of the workbook. To prevent this, I created a macro to execute the Tab key instead which works great. The problem is, if I am in another workbook (Book1.xls) and Productivity.xls is open and I press enter or down, the macro runs and my active cell Tabs across the screen of Book1 instead of going down.

In the ThisWorkbook object (in Productivity.xls) I have the following code:

Private Sub Workbook_Open()
Application.OnKey "~", "PressedEnter"
Application.OnKey "{ENTER}", "PressedEnter"
Application.OnKey "{DOWN}", "PressedEnter"
End Sub

And in a regular Module in the same workbook, I have this:

Private Sub PressedEnter()
Application.SendKeys "{TAB}"
End Sub

Does anyone have any suggestions on how I can make sure the macro from the Productivity workbook doesn't run in any other workbook I have open at the same time?

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Put your code in the workbook "Activate" event. Then write the opposite procedure (to turn off the key intercepts) and put that in the workbook "DeActivate" event.

Gary
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,548
Members
452,927
Latest member
rows and columns

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