+ Reply to Thread
Results 1 to 10 of 10

Code works in VBA but not in worksheet?

  1. #1
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Code works in VBA but not in worksheet?

    When I run the code from VBA (pressing F5), it runs perfectly, BUT when I run it from a check box in worksheet, it displays a prompt "the chart is on a protected sheet"? After I click OK, it displays the chart. It works perfectly in other files? Thanks in advance.


    [CODE]Sub ChangeChartTypeFuelHistory()
    Application.ScreenUpdating = False

    Worksheets("Fuel History Chart").Unprotect Password:="1345t"

    Dim Cht As ChartObject
    For Each Cht In Worksheets("Fuel History Chart").ChartObjects
    If Cht.Chart.ChartType = xlLine Then
    Cht.Chart.ChartType = xlColumnClustered
    Else
    With Cht
    .Chart.ChartType = xlLine
    .Chart.SetElement (msoElementDataLabelTop)
    End With
    End If
    Next Cht

    Worksheets("Fuel History Chart").Protect Password:="1345t"

    Application.ScreenUpdating = True
    End Sub[CODE]

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: Code works in VBA but not in worksheet?

    Hi Aland2929

    Please take a moment to read our forum rules...here
    Your post does not comply with Rule # 2
    2. Programming code must be enclosed in code tags to improve readability. (A, Z)
    Please Login or Register  to view this content.
    So...Edit your post...Highlight the code and press the # button
    We can only assist once this has been done…
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Code works in VBA but not in worksheet?

    I would try

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Code works in VBA but not in worksheet?

    @sintek: apologies, posts overlapped.

  5. #5
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: Code works in VBA but not in worksheet?

    Thanks for your reply - unfortunately that does not work? Can't understand why this code works perfectly in other files?

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Code works in VBA but not in worksheet?

    Once you have corrected your code tags, as requested, post example workbook
    Cheers
    Andy
    www.andypope.info

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: Code works in VBA but not in worksheet?

    Quote Originally Posted by TMS View Post
    @sintek: apologies, posts overlapped.
    No worries...Get into trouble by the big boss...I do not...
    Last edited by sintek; 07-19-2020 at 12:14 PM.

  8. #8
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: Code works in VBA but not in worksheet?

    Hi Andy, I think my code is enclosed in tags?

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: Code works in VBA but not in worksheet?

    It is, but the one at the end needs to be [/CODE]

  10. #10
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: Code works in VBA but not in worksheet?

    Thanks to all for replies - check box cell reference was protected - problem now solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Code works in the Module1 but not in the Worksheet?
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-09-2015, 03:05 PM
  2. Replies: 1
    Last Post: 12-20-2014, 03:36 AM
  3. Code fails when ran within Userform, but works when ran from Macro button on worksheet.
    By Nitefox in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 6
    Last Post: 11-29-2013, 02:28 AM
  4. Replies: 7
    Last Post: 08-06-2013, 02:23 PM
  5. [SOLVED] Code Gives error if I run it from Module but works fine when run it from This Worksheet
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-17-2013, 01:18 PM
  6. Replies: 10
    Last Post: 04-18-2012, 10:33 AM
  7. VBA Code works but not on worksheet
    By elabounty in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-14-2011, 03:00 PM

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.6.0 RC 1