1

Currently working on a vba script that makes charts automatically. I would like to add a datatable which is done using: .HasDataTable = True

However I would like to show the values of series as percentages. Currently the value is defined as a Double containing all the values but not the right formatting. enter image description here

Using Format() or FormatPercent() will give the right values but returned in a String. This works for the datatable but not for the chart itself since it doesn't recognize the values anymore.

My question comes down to whether it is possible to show the values as percentages in both the datatable and the chart? Without VBA it is easily done by formatting the data in the cells itself. The problem is that for formatting a String is returned but for the graph Integers or Doubles are needed.

Any help is welcome and please let me know if the question is unclear.

Below is part of the code. If I dim Ratio as String and use FormatPercent() I get the requested formatting but then the values in Ratio ar no longer doubles so it doesn't give the required chart.

Dim Ratio() As Double   
Dim labels() As String

ReDim Ratio(1 To Height)
ReDim labels(1 To Height)


For Each Column In sArray
        labels(i) = Sheets(DataSheetName).Cells(LabelsRow, Column)
        Ratio(i) = Math.Round(Sheets(DataSheetName).Cells(LabelsRow + 3, Column), 2)
        i = i + 1
Next Column


Set myChtObj = Sheets(DrawSheetName).ChartObjects.Add(Left:=Left, Width:=Width, Top:=Top, Height:=HeightGraph)

Dim srsNew1 As Series

 ' Add the chart
With myChtObj.Chart
    .ChartArea.Fill.Visible = False
    .ChartArea.Border.LineStyle = xlNone
    .PlotArea.Format.Fill.Solid
    .PlotArea.Format.Fill.Transparency = 1
    .HasTitle = True
    .ChartTitle.text = Title
    .HasLegend = False

    .Axes(xlValue).TickLabels.NumberFormat = "0%"
    .Axes(xlCategory, xlPrimary).HasTitle = False

    'add data table
    .HasDataTable = True

     ' Make Line chart
    .ChartType = xlLine


     ' Add series
    Set srsNew1 = .SeriesCollection.NewSeries
    With srsNew1
        .Values = Ratio
        .XValues = labels
        .Name = "Ratio"
        .Interior.Color = clr3 'RGB(194, 84, 57)
    End With


End With

In adiition to the above. The picture below might illustrate best what I want.

enter image description here

11
  • To convert a string into the double you can use the VBScript function CDbl. Please let me know if the answer is unclear :) Commented Nov 10, 2015 at 15:26
  • Hi duDE, thanks for responding. That does work if the value in the string would be a true double, for example 2.67. However the string now contains the value 267% which can't be converted back to a double. Maybe I should rephrase my question, the chart should contain the true value which is a double. But in addition I would like the datatable to be formatted to show the values as percentages
    – WhoKnows19
    Commented Nov 10, 2015 at 15:39
  • 2
    Can you add the appropriate part of this script to your question? It would be helpful. Commented Nov 10, 2015 at 16:31
  • Just drop the % from it and use .format Commented Nov 10, 2015 at 17:32
  • 1
    I'm sorry, I'm confused. You use each column to populate the dblArr and strArr from a sheet. Then you create a new series with those arrays, yes? And the issue is that your strArr doesn't match its axis' format? Commented Nov 11, 2015 at 11:52

0

You must log in to answer this question.

Browse other questions tagged .