One workbook's sheet selection change code is running in all opened workbooks.

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey,

I have the following code that runs inside a workbook category of workbook A

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.OnKey "{b}", "search_b"
End Sub

and the following code that runs inside a module of the same workbook, workbook A.

Code:
Sub search_b()
   Dim Fnd As Range
   Set Fnd = Range("H:H").Find("b*", , xlValues, xlWhole, , , False, False)
If Not Fnd Is Nothing Then
  Application.Goto Cells(Fnd.Row, ActiveCell.Column), scroll:=True
End If
End Sub

Now if I open a new workbook, workbook B, and type in letter b and the code from workbook A will run in the newly opened workbook B.
If I close workbook A and then type b on workbook B. workbook A will open up by itself and run the code on the workbook B.

How do I stop this behaviour and run the code in only that workbook where the code resides? meaning in workbook A.

will appreciate help on this.
ty
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
In the help section for OnKey, MS says it continues to run until you run Onkey again with no parameters. Meaning, Onkey alters the keyboard. The letter b now wants to run the macro in that workbook. You will have to Run Onkey without any parameters in the "Private Sub Workbook_BeforeClose(Cancel As Boolean)" SUB.

Jeff
 
Upvote 0
In the help section for OnKey, MS says it continues to run until you run Onkey again with no parameters. Meaning, Onkey alters the keyboard. The letter b now wants to run the macro in that workbook. You will have to Run Onkey without any parameters in the "Private Sub Workbook_BeforeClose(Cancel As Boolean)" SUB.

Jeff

How about

Code:
Private Sub Workbook_Activate()
Application.OnKey "{b}", "search_b"
End Sub


Private Sub Workbook_Deactivate()
Application.OnKey "{b}"
End Sub

edit: figured it out

Thank you.
 
Last edited:
Upvote 0
That'll work! ... and since Deactivate also gets triggered when you close the workbook, it should be good.
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,655
Members
448,975
Latest member
sweeberry

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