VBA error activating a chart on a protected worksheet

tj4242

New Member
Joined
Dec 26, 2017
Messages
42
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have a worksheet that has cells and charts on it. When the worksheet is unprotected everything works perfect. When I try to protect some cells, the VBA crashes on this line

VBA Code:
ActiveSheet.ChartObjects(chartname).Activate

The error is Run-time error '1004'. Application-defined or object defined error.

If I enter the password and unprotect the sheet, everything goes back to working. Any suggestions?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

This error is likely occurring because the ChartObjects collection is not accessible when the sheet is protected. When a sheet is protected, certain actions, such as modifying or activating objects, are not allowed by default.

One solution would be to unprotect the sheet before activating the chart, and then protect it again afterwards. This can be done using the Unprotect and Protect methods of the Worksheet object, like this:

VBA Code:
ActiveSheet.Unprotect (password)
ActiveSheet.ChartObjects(chartname).Activate
ActiveSheet.Protect (password)

Note that you would need to replace "password" with the actual password used to protect the sheet.

Another option is to use the AllowFormattingCells and AllowUsingObjects properties of the Protect method to allow specific actions like formatting cells and using chart objects even when the sheet is protected.

VBA Code:
ActiveSheet.Protect(password, AllowFormattingCells:=True, AllowUsingObjects:=True)

You should also check that the chartname exists and that you have the correct name and that you are pointing to the correct chart.

Hope this helps!
 
Upvote 0
Solution
BTW, this worked perfectly.

VBA Code:
ActiveSheet.Unprotect (password)
ActiveSheet.ChartObjects(chartname).Activate
do other stuff
ActiveSheet.Protect (password)
 
Upvote 0
Super. Can you close as a solution please. Anything else let me know
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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