+ Reply to Thread
Results 1 to 5 of 5

Thread: Chart Macro Fails When Sheet Is Protected

  1. #1
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2010
    Posts
    132

    Chart Macro Fails When Sheet Is Protected

    Hello
    The macro to create the chart works fine all the time the sheet's unprotected. As soon as it's protected, it reports a runtime error 1004. Specified value out of range.
    I've Googled trying to find an answer, but they're all as clear as mud to me.
    Attached is the macro debug screen
    Running Excel 2010
    Thanks
    Attached Images Attached Images
    Last edited by Philb1; 09-16-2011 at 05:12 PM.

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,564

    Re: Chart Macro Fails When Sheet Is Protected

    Hi,

    Add a first line to your macro code
    ActiveSheet.Unprotect
    and add a last line
    ActiveSheet.Protect
    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: Chart Macro Fails When Sheet Is Protected

    Hi Richard, Thanks for the reply
    It worked. But. The macro requires the password to unprotect the sheet, it creates the chart, pastes it onto another sheet & then protects the wrong sheet.
    I would like it to not require the password & to re lock the sheet the macro started from. I've uploaded a sample for you to play with
    Thanks
    Attached Files Attached Files

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,564

    Re: Chart Macro Fails When Sheet Is Protected

    Hi,

    Try the attached

    Sub Macro1()
        Dim shChart As Worksheet, oChart As Object
    
        Set shChart = Sheet1
        shChart.Unprotect Password:="help"
        Set oChart = shChart.Shapes.AddChart
        oChart.Select
        ActiveChart.ApplyChartTemplate ( _
                                       "C:\Users\Phil\AppData\Roaming\Microsoft\Templates\Charts\DefaultChart1.crtx")
        ActiveChart.SetSourceData Source:=Range("Charts!$B$3:$D$10")
        oChart.Cut
        With Sheets("View").Range("F3")
            .Parent.Paste
        End With
    
        shChart.Protect Password:="help"
    End Sub
    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  5. #5
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: Chart Macro Fails When Sheet Is Protected

    Thanks for your help Richard
    I came up with this
     Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
        ActiveSheet.Unprotect "help"
        Set MyPlotRange = Selection
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ApplyChartTemplate ( _
            "C:\Users\Phil\AppData\Roaming\Microsoft\Templates\Charts\DefaultChart1.crtx")
        ActiveChart.SetSourceData Source:=MyPlotRange
        ActiveChart.Parent.Cut
        Sheets("View").Select
        Range("F3").Select
        ActiveSheet.Paste
        Sheets("Charts").Select
        ActiveSheet.Protect "help"
        Sheets("View").Select
    End Sub
    It works. I'll try your solution too
    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0