图表数据错误

时间:2014-06-25 07:22:06

标签: excel-vba vba excel

如果我的图表数据仅等于一列,则会出错。

Sub RemoveHiddenColumns()
    Dim myChart As Chart
    Set myChart = Chart4

    myChart.Activate    'first activate the chart

    Dim i As Integer

    For i = 1 To ActiveChart.SeriesCollection.Count    'loop through each series

        Dim strText As String, strCol As String, strSht As String, intCol As Integer

        strText = Split(ActiveChart.SeriesCollection(i).Formula, ",")(2)    'extract sheet name and column of series
        strSht = Split(strText, "!")(0)
        strCol = Split(strText, "!")(1)    'get column range of series
        Dim wks As Worksheet
        Set wks = Sheet2
        If wks.Range(strCol).EntireColumn.Hidden = True Then    'if the column is hidden
            ActiveChart.SeriesCollection(i).Delete    'remove the series
        End If
    Next

End Sub

1 个答案:

答案 0 :(得分:0)

此代码成功从表中删除隐藏的ROWS。我知道你想要删除列,但我不确定你的目的是什么,所以我没有尝试适应它。你应该能够相当容易地做到这一点。

Sub RhidRow2(ByVal count4 As Long)
    Dim count1 As Long 'counters to be used
    Dim ws As Worksheet
    Dim rngVis As Range
    Dim rngDel As Range
    Set ws = ActiveSheet

    On Error Resume Next
    Set rngVis = ws.Range("A2:A" & count4).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rngVis Is Nothing Then
        ws.Range("Z1").Value = 1
    Else

        For count1 = count4 To 2 Step -1
            If ws.Rows(count1).Hidden = True Then
                If rngDel Is Nothing Then
                    Set rngDel = ws.Rows(count1)
                Else
                    Set rngDel = Union(rngDel, ws.Rows(count1))
                End If
            End If
        Next count1

    If Not rngDel Is Nothing Then
        Application.DisplayAlerts = False
        Intersect(rngDel, rngDel.ListObject.DataBodyRange).Delete 'if row is hidden, delete
        Application.DisplayAlerts = True
    End If

    End If
End Sub