使用VBA excel在Stacked面积图中修复图表标签颜色

时间:2017-07-24 11:28:38

标签: excel vba charts

我正在尝试创建一个允许用户选择“线”或“堆积”区域图表的仪表板。每个标签(类别)的颜色代码已经预先确定。图表格式的代码适用于折线图。但是,当我选择Stacked-Area图表可视化时,它不会。以下是折线图的代码:

Public Function ChartSeriesFormat()

Dim objDictChartColor   As Object       
Dim varChartColor       As Variant      
Dim rngChartColor       As Range        
Dim objSalesChart       As Chart         
Dim rngTemp             As Range        
Dim strSeriesName       As String       
Dim lngStartRow         As Long        
Dim lngColor            As Long         
Dim lngItem             As Long         

' --> Assign objects
Set objSalesChart = shtOutput.ChartObjects("chtMarketShare").Chart
varChartColor = shtMapping.Range("rng_ChartColor").CurrentRegion
Set rngChartColor = shtMapping.Range("rng_ChartColor").CurrentRegion
Set objDictChartColor = CreateObject("Scripting.Dictionary")

For lngItem = 1 To UBound(varChartColor, 1)
    If Not objDictChartColor.exists(LCase(varChartColor(lngItem, 1))) Then
        objDictChartColor.Add LCase(varChartColor(lngItem, 1)), rngChartColor.Cells(lngItem, 1).Interior.Color
    End If
Next lngItem

lngStartRow = shtOutput.Range("rng_OutputDB1").CurrentRegion.Rows.Count

For lngItem = 1 To objSalesChart.SeriesCollection.Count
    With objSalesChart
        'Make all the lines in the series xlcontinous
        .SeriesCollection(lngItem).Border.LineStyle = xlContinuous
        strSeriesName = LCase(.SeriesCollection(lngItem).Name)
        lngColor = objDictChartColor.Item(strSeriesName)
        .SeriesCollection(lngItem).Border.Color = lngColor
    End With
Next

On Error Resume Next
    With shtOutput.Range("rng_OutputDB2")
        If .Offset(1).Value <> "" Then
            Set rngTemp = .CurrentRegion.SpecialCells(xlCellTypeVisible)
        End If
    End With
    If Err.Number <> 0 Then
        Err.Clear: On Error GoTo 0: On Error GoTo -1
        GoTo ClearMemory
    End If
Err.Clear: On Error GoTo 0: On Error GoTo -1


For lngItem = lngStartRow To objSalesChart.SeriesCollection.Count
    With objSalesChart
        'Make all the lines in the series xlcontinous
        .SeriesCollection(lngItem).Border.Color = objSalesChart.SeriesCollection(lngItem - (lngStartRow - 1)).Border.Color
        .SeriesCollection(lngItem).Border.LineStyle = xlDot
    End With
Next

ClearMemory:

结束功能

稍后添加了选择“堆积区域”图表的选项。有什么帮助吗?

0 个答案:

没有答案