+ Reply to Thread
Results 1 to 3 of 3

Thread: Format pie-chart in VBA

  1. #1
    Registered User
    Join Date
    03-16-2010
    Location
    Brasil
    MS-Off Ver
    Excel 2003
    Posts
    7

    Format pie-chart in VBA

    Hi all,

    My doubt is probably simple for most of you, but I'm pretty new at this so, better to ask you than keep looking through google.

    I've edited a code in word to create a pie chart and format it according the name of the colors using the select case. When this code is run in a excel 2007 it runs pretty good, but once I write the same code in a excel 2003, it gives me a runtime error which say: "Object variable or With block variable not set". below in the code I show where this message appears.

    My problem is right in this section, how do I solve this issue in excel 2003, once in excel 2007 is working properly.

    Thanks in advance

    The code is:
    Sub Pie_Chart()
    'Choosing what type of chart
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xl3DPieExploded
        ActiveChart.ApplyLayout (6)
        ActiveChart.ChartArea.Select
    'Creating the Chart
      With ActiveChart.SeriesCollection.NewSeries
            .Name = ActiveSheet.Range("C2")
            .Values = ActiveSheet.Range("C8:C10")
            .XValues = ActiveSheet.Range("B8:B10")
      End With
    'Placing the Chart
      With ActiveChart.Parent
        .Left = 1
        .Width = 215
        .Top = 267
        .Height = 150
      End With
    'Formating the Chart
       Dim iPoint As Long, nPoint As Long
      With ActiveChart.SeriesCollection(1).Select 'THE RUN TIME ERROR ON EXCEL 2003 COMES RIGHT HERE
         For iPoint = 1 To .Points.Count
              Select Case WorksheetFunction.Index(.XValues, iPoint)
                     Case "Yellow"
                        .Points(iPoint).Interior.ColorIndex = 6 ' Yellow
                     Case "Red"
                        .Points(iPoint).Interior.ColorIndex = 3 ' Red
                     Case "Green"
                        .Points(iPoint).Interior.ColorIndex = 4 ' Green
               End Select
           Next
       End With
    End Sub

  2. #2
    Forum Contributor
    Join Date
    03-12-2009
    Location
    Edmonton, Canada
    MS-Off Ver
    Excel 2003(work), Excel 2007(home)
    Posts
    182

    Re: Help with a pie-chart format in a VBA code

    try just getting rid of the .select in your with statement
    If you can't figure out how a formula works, try stepping through it using "Evaluate Formula" in the Formula Auditing menu item in the tools menu!

    If you want to see where your code went wrong, try stepping through it by clicking in the code and pressing F8 and watch as the magic happens!


    If you are happy with any of the results, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  3. #3
    Registered User
    Join Date
    03-16-2010
    Location
    Brasil
    MS-Off Ver
    Excel 2003
    Posts
    7

    Thumbs down Help with a pie-chart format in a VBA code

    Actually, I thought taking the .select out would solved the issue, because that's what I did in my excel 2007 file. Although in this 2003 file, it happens the same run time error 91.

    Any other idea?
    Last edited by Leosado; 03-19-2010 at 08:14 AM.

+ 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