我正在使用vba创建图表。为此,我使用以下代码:
Function CreateChart(start, finish, chartPos) As Integer
Dim objChart As ChartObject
Dim myChtRange As Range
Dim myDataRange As Range
Dim seri As Series
With Application.ActiveWorkbook.Sheets("Charts")
' What range should chart cover
'Set myChtRange = Application.InputBox( _
prompt:="Select a range where the chart should appear.", _
Title:="Select Chart Position", Type:=8)
' What range contains data for chart
Set myChtRange = Application.ActiveWorkbook.Sheets("Charts").Range("A" & chartPos & ":F" & chartPos + 19)
'Set myDataRange = Application.InputBox( _
prompt:="Select a range containing the chart data.", _
Title:="Select Chart Data", Type:=8)
Set myDataRange = Application.ActiveWorkbook.Sheets("Consolidation").Range("A" & start - 1 & ":B" & finish - 1)
' Cover chart range with chart
Set objChart = .ChartObjects.Add( _
Left:=myChtRange.Left, Top:=myChtRange.Top, _
Width:=myChtRange.Width, Height:=myChtRange.Height)
' Put all the right stuff in the chart
With objChart.Chart
.ChartArea.AutoScaleFont = False
.ChartType = xlColumnClustered
.SetSourceData myDataRange
.HasTitle = True
.ChartTitle.Characters.text = Application.ActiveWorkbook.Sheets("Consolidation").Range("B" & start - 1).Value
.ChartTitle.Font.Bold = True
.ChartTitle.Font.Size = 18
End With
If Not cType = "Round" Then
objChart.Chart.Legend.Delete
End If
If cType = "Points" Then
objChart.Chart.ApplyDataLabels xlDataLabelsShowLabel
End If
Set seri = objChart.Chart.SeriesCollection(1)
If Not cType = "Points" Then
seri.HasDataLabels = True
End If
End With
CreateChart = chartPos + 25
End Function
“start”和“finish”是表开始和结束的行,它们总是有2列(A,值和B,多少次)在大多数情况下它完美地运行。例如,对于此表,它给出了这个图表:
但是,如果可能的值是一个数字,那么它就会错误,正如您在此示例中所看到的那样:
结果是可能的答案。如果找到改变它的方式以正确行为单击右键并选择charType但不使用vba,我想用vba更正它。有没有办法实现呢?
答案 0 :(得分:0)
您是否可以将其更改为字符串" 1"," 2"等等...-? 我不知道这会对你的图表有什么影响,因为我不经常使用它们......
.NumberFormat =" @"