【发布时间】:2020-01-27 14:14:29
【问题描述】:
我希望根据另一个工作表中单元格的值更改每个系列的颜色。
我通过单步执行宏进行了检查,除了颜色更改之外的所有内容都正常工作。这种类型的代码将针对不同类型的事件运行六次(我删除了其中的五个事件以缩短本文的篇幅)。
我在任何系列上都没有标记,只有该系列两点之间的连接线。
Sub test()
Dim LastRowB As Long
Dim LastRowJ As Long
Dim LastRowR As Long
Dim LastRowZ As Long
Dim LastRowAH As Long
Dim LastRowAP As Long
Dim chartj As Variant
Dim wb As Workbook
Dim ws As Worksheet
Set ws6 = Sheet6
'Set chart axis to approximately the range required
Chart7.Axes(xlCategory).MinimumScale = 43000
Chart7.Axes(xlCategory).MaximumScale = 44500
'Set graph ranges as per sheet
Chart7.Axes(xlCategory).MinimumScale = ws6.Range("D9").Value
Chart7.Axes(xlCategory).MaximumScale = ws6.Range("D10").Value
'All subsequent examples follow template for fills
'--------------------------- Fill ------------------------------
'Find first occurence of -. There should be a - under all arrays to ensure that a stop ocurs or this will get stuck in a loop if all entries are filled
'i.e. 15 fill batches are performed and no - will appear in the table.
LastRowB = ws6.Cells.Find(What:="-", _
After:=ws6.Range("B15"), _
LookAt:=xlWhole, _
LookIn:=xlValues, _
searchorder:=xlByColumns, _
searchdirection:=xlNext, _
MatchCase:=False).Row
' Set the integers j = 1 and runs all loops up until a max of 15 occurences based off of i. This is cause there are 15 Fills series plotted on the graph.
' i = 16 as arrays start in row 16. And this runs until the number of row that the last dash is found.
Dim i As Integer
Dim j As Integer
j = 1
For i = 16 To LastRowB
If i = LastRowB Then
'if we have hit the last row number then we end the if and move on
Else
Set chartj = Chart7.SeriesCollection(j)
chartj.Select
'This .select is to confirm that the correct series is being selected. Can be removed.
'Check to see if the series should be green or red colour from the colour column
If ws6.Range("F" & i).Value = "Green" Then
'Format the connecting lines as green
chartj.Format.Line.Visible = msoFalse
chartj.Format.Line.Visible = msoTrue
chartj.Border.LineStyle = xlContinuous
chartj.Border.Color = RGB(0, 176, 80)
Else
'Format the connecting lines as red
chartj.Format.Line.Visible = msoFalse
chartj.Format.Line.Visible = msoTrue
chartj.Border.LineStyle = xlContinuous
chartj.Border.Color = RGB(255, 0, 0)
End If
End If
j = j + 1
Next i
Windows 10 中的 Excel 2013。
【问题讨论】:
-
chartj.Format.Line.Visible = msoFalse后面紧跟chartj.Format.Line.Visible = msoTrue的目的是什么,为什么这些行会出现在If语句的两个分支中? -
感谢您的回复 jsheeran。我发现另一篇文章解释了一些关于在 excel 系列here 上调整颜色的问题。它描述了使用 .visible=msoFalse 和 .visible=msoTrue 语句欺骗系统的需要。正如我所指出的,我试图根据自己的需要调整此代码,但不幸的是无法让事情正常工作。