目前正在开发一个自动生成图表的vba脚本。我想添加一个数据表,使用:.HasDataTable = True
完成但是我想将系列的值显示为百分比。目前,该值定义为包含所有值但不包含正确格式的Double。使用Format()或FormatPercent()将给出正确的值,但在String中返回。这适用于数据表但不适用于图表本身,因为它不再识别这些值。
我的问题归结为是否可以在数据表和图表中显示值作为百分比?如果没有VBA,可以通过格式化单元格本身的数据来轻松完成。问题是,为了格式化,返回了String,但是对于图形,需要整数或双打。
以下是代码的一部分。如果我将Ratio作为字符串调暗并使用FormatPercent()我得到所请求的格式,但是比率ar中的值不再加倍,因此它不会给出所需的图表。
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