How to differentiate when a sheet is selected manually or via VBA

Rodanenb

New Member
Joined
Aug 10, 2013
Messages
3
I have a sheet ("SheetName") which contains a warning pop-up when it is selected manually, using the code below:

Private sub Worksheet_Activate()

If Msgbox("warning text",vbOkOnly)=7 then
end if

End Sub

which works fine when the sheet is selected.

However, I also have another macro which updates the data in this sheet from a masterfile, and when the code below is run:

Sheets.("SheetName").Select

this also triggers the warning. Is there any way to differentiate if the worksheet selection is manual by the user or triggered by another line of VBA code?

Thanks

Robert
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi and welcome to the forum,

In your other macro, you can temporarily disable events, something like this:

Code:
Application.EnableEvents = False


Sheets("SheetName").Select


Application.EnableEvents = True
By the way, for your warning message code, this should suffice (unless there is something I'm missing):

Code:
[I]Private sub Worksheet_Activate()

[/I]MsgBox "warning text", vbOKOnly[I]

End Sub[/I]
Also, note that you rarely need to select sheets to work with them, so you might be able to avoid the issue altogether if you can adjust your code to reflect that.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,702
Members
449,048
Latest member
81jamesacct

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