Macro Assigned to Shape Changes Active Sheet

RB2129

New Member
Joined
Apr 18, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a macro that changes the decimal places shown on a column of data in two worksheets. Running the macro from View Macros works fine.

Once the macro is assigned to a shape or form control, running the macro then changes the active sheet.

This only occurs when the macro unprotects then protects the sheets but this is required for the macro to function.

I tried removing the Unprotect and Protect code from the macro. The active sheet does not change but the sheets are protected so this cannot be removed.

The issue only occurs both when the protection is on and when the macro is assigned to a shape or to a form control.

The following recreates the problem. Run from Sheet1 via a shape or form control, active sheet stays as Sheet1. Run from a shape in Sheet2, active sheet moves to Sheet1.

Dim ws As Worksheet
Set ws = Sheets("Sheet1")
ws.Unprotect Password:="test"
ws.Range("B3").NumberFormat = "#,##0"
ws.Protect Password:="test"

Set ws = Sheets("Sheet2")
ws.Unprotect Password:="test"
ws.Range("B3").NumberFormat = "#,##0"
ws.Protect Password:="test"
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi and welcome to the forum.
Don't know why that happens but you could workaround this issue as follows:


Dim oSh As Worksheet
Dim ws As Worksheet

Application.ScreenUpdating = False
Set oSh = ActiveSheet.Shapes(Application.Caller).Parent


Set ws = Sheets("Sheet1")
ws.Unprotect Password:="test"
ws.Range("B3").NumberFormat = "#,##0"
ws.Protect Password:="test"

Set ws = Sheets("Sheet2")
ws.Unprotect Password:="test"
ws.Range("B3").NumberFormat = "#,##0"
ws.Protect Password:="test"

oSh.Activate
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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