
HOW TO: Create SPEEDOMETER GRAPH - VBA
WOOOOHOOO. Simple Way to write a Speedograph code in VBA. It really isnt the best way to do this, but it works.
Sub Speed_graph()
For Each e In ThisWorkbook.Sheets(1).Shapes
e.Delete
Next
Dim Speed() As Variant
Dim Pointer() As Variant
Dim Cht As Object
pct = 33 / 160
pct = pct * 180
Speed = Array(0, 15, 40, 45, 100)
Pointer = Array(pct, 2, 180)
ActiveSheet.Shapes.AddChart.Select
Set Cht = ActiveChart
Cht.ChartType = xlDoughnut
Cht.SeriesCollection.NewSeries
Cht.SeriesCollection(1).Name = "Speed"
Cht.SeriesCollection(1).Values = Speed
Cht.ChartGroups(1).FirstSliceAngle = 272
Cht.SeriesCollection.NewSeries
Cht.SeriesCollection(2).Name = "Pointer"
Cht.SeriesCollection(2).Values = Pointer
Cht.Legend.Delete
x = 0
For Each pt In Cht.SeriesCollection(1).Points
With pt.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Visible = msoTrue
.Transparency = 0
.Solid
If x = 1 Then
.ForeColor.RGB = vbRed
End If
If x = 2 Then
.ForeColor.RGB = vbYellow
End If
If x = 3 Then
.ForeColor.RGB = vbGreen
End If
If x = 4 Then
.Visible = msoFalse
End If
End With
x = x + 1
Next
Cht.ChartGroups(1).FirstSliceAngle = 272
Cht.SeriesCollection(2).Points(1).Format.Fill.Visible = msoFalse
Cht.SeriesCollection(2).Points(3).Format.Fill.Visible = msoFalse
Cht.SeriesCollection(2).AxisGroup = 2
Cht.SeriesCollection(2).Points(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = vbBlack
.Solid
End With
Cht.SeriesCollection(2).Points(3).Select
Cht.ChartGroups(2).FirstSliceAngle = 274
End Sub
コメント