Consulting

Results 1 to 4 of 4

Thread: Select and Edit certain type of ChartObject in VBA

  1. #1
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    5
    Location

    Select and Edit certain type of ChartObject in VBA

    Dear All,

    Appreciate if someone can help me on this code,
    Basically I want to edit the axis value in certain type of Chart
    But this code didn't worked.

    Dim objCht As ChartObject

    For Each objCht In ActiveSheet.ChartObjects

    If ActiveChart.ChartType = xlBarStacked Then

    With objCht.Chart
    With .Axes(xlValue)
    .MaximumScale = ActiveSheet.Range("M8").Value
    .MinimumScale = ActiveSheet.Range("M9").Value
    End With
    End With

    Else

    With objCht.Chart
    With .Axes(xlCategory)
    .MaximumScale = ActiveSheet.Range("M8").Value
    .MinimumScale = ActiveSheet.Range("M9").Value
    End With
    End With

    End If

    Next objCht

    Thanks in advance

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Change the only instance of ActiveChart to objCht.Chart

    Aside from that you could, if you wanted, shorten the code a bit:
    Dim objCht As ChartObject
    
    For Each objCht In ActiveSheet.ChartObjects
      With objCht.Chart
        If .ChartType = xlBarStacked Then myAxis = xlValue Else myAxis = xlCategory
        With .Axes(myAxis)
          .MaximumScale = ActiveSheet.Range("M8").Value
          .MinimumScale = ActiveSheet.Range("M9").Value
        End With
      End With
    Next objCht
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie dangelor's Avatar
    Joined
    Aug 2014
    Location
    Indiana USA
    Posts
    4
    Location
    It might help if you explained what in your code didn't work as expected.
    ___________________________________________________________
    If I've been helpful, let me know. If I haven't, let me know that too. -Rich

  4. #4
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    5
    Location
    Hello p45cal

    It works perfectly

    Many Thanks for your help

Posting Permissions

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