以下Excel VBA模块旨在根据条件更改各个线段的颜色。它的效果很好,除了它不会绘制单独的线标记。问题显然存在于每个xlMarkerStyleCircle,MarketSize,MarkerBackgroundColor和MarkerForegroundColor行中。我不确定问题是否与对象引用的不正确对象命名或不正确的排序有关。任何帮助或建议将不胜感激。同样,如果有人看到更有效的编码方法,请随时分享。
谢天谢地......
干杯, 约翰
Sub tropical_cyclone_track_format()
With ActiveSheet
Set r = .Range("E24:E31")
For i = 1 To .Shapes("CHART 3").Chart.SeriesCollection(1).Points.Count - 1
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerStyle = xlMarkerStyleCircle
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerSize = 2
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerBackgroundColor = RGB(0, 0, 0)
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerForegroundColor = RGB(0, 0, 0)
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.LineStyle = xlContinuous
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.Color = RGB(255, 255, 64)
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Transparency = 0
If r(i) = "1" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Weight = 1
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerStyle = xlMarkerStyleCircle
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerSize = 2
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerBackgroundColor = RGB(0, 0, 0)
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerForegroundColor = RGB(0, 0, 0)
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.LineStyle = xlContinuous
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.Color = RGB(255, 153, 16)
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Transparency = 0
If r(i) = "2" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Weight = 1
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerStyle = xlMarkerStyleCircle
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerSize = 2
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerBackgroundColor = RGB(0, 0, 0)
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerForegroundColor = RGB(0, 0, 0)
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.LineStyle = xlContinuous
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.Color = RGB(255, 3, 0)
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Transparency = 0
If r(i) = "3" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Weight = 1
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerStyle = xlMarkerStyleCircle
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerSize = 2
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerBackgroundColor = RGB(0, 0, 0)
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).MarkerForegroundColor = RGB(0, 0, 0)
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.LineStyle = xlContinuous
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border.Color = RGB(80, 0, 0)
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Transparency = 0
If r(i) = "4" Then .Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Format.Line.Weight = 1
Next i
End With
End Sub
答案 0 :(得分:0)
这是半答案,因为它解释了为什么你的代码没有绘制单独的行标记,但没有给你正确的方法。如果您希望我仔细查看您的问题,您必须提供工作表,以便我可以评估图表的详细信息。
另一方面,您的代码非常重复,因此效率低下。
我现在讨论这两个问题。
Point
对象没有Border
方法。这就是原因
为什么你的代码不起作用。这意味着您无法编写代码:
.Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1).Border
如果您想知道Point
对象有哪些方法,请在VBE上按F2。如果您想了解有关对象和方法的更多信息,可以尝试analysistabs.com或dummies.com,然后excel-spreadsheet.com。同样,如果您共享工作表,我将能够为您提供帮助。
你重复32次代码
.Shapes("CHART3").Chart.SeriesCollection(1).Points(i + 1)
你可以
通过声明p
类型的变量Point
并分配来避免这种情况
它的值在for循环之后。我给你一个ideia你怎么能实现这个,但为了它正常工作,你应该分享你的工作表。
Sub tropical_cyclone_track_format()
Dim r As Range
Set r = ActiveSheet.Range("E24:E31")
Dim i As Integer
For i = 1 To ActiveSheet.Shapes("CHART 3").Chart.SeriesCollection(1).Points.Count - 1
Dim p As Point
Set p = ActiveSheet.Shapes("CHART 3").Chart.SeriesCollection(1).Points(i + 1)
If r(i) = "1" Then
With p
p.MarkerSize = xlMarkerStyleCircle
p.MarkerStyle = 2
p.MarkerBackgroundColor = RGB(0, 0, 0)
p.MarkerForegroundColor = RGB(0, 0, 0)
p.Format.Line.Transparency = 0
p.Format.Line.Weight = 1
End With
End If
If r(i) = "2" Then
With p
p.MarkerSize = xlMarkerStyleCircle
p.MarkerStyle = 2
p.MarkerBackgroundColor = RGB(0, 0, 0)
p.MarkerForegroundColor = RGB(0, 0, 0)
p.Format.Line.Transparency = 0
p.Format.Line.Weight = 1
End With
End If
If r(i) = "3" Then
With p
p.MarkerSize = xlMarkerStyleCircle
p.MarkerStyle = 2
p.MarkerBackgroundColor = RGB(0, 0, 0)
p.MarkerForegroundColor = RGB(0, 0, 0)
p.Format.Line.Transparency = 0
p.Format.Line.Weight = 1
End With
End If
If r(i) = "4" Then
With p
p.MarkerSize = xlMarkerStyleCircle
p.MarkerStyle = 2
p.MarkerBackgroundColor = RGB(0, 0, 0)
p.MarkerForegroundColor = RGB(0, 0, 0)
p.Format.Line.Transparency = 0
p.Format.Line.Weight = 1
End With
End If
Next i
End Sub