特定工作表值范围的循环图更改

时间:2014-05-11 17:23:48

标签: excel vba loops excel-vba

以下代码删除图表中的线段。我想重复运行它来获取一行值而不是$ A $ 1的单个值:

    With Sheet1.ChartObjects("Chart 1").Chart.SeriesCollection(1)
        .Points(Sheet1.Range("$A$1").Value).Format.Line.Visible = msoFalse
    End With

虽然这是不正确的语法,但总体目标可能如下所示:

    With Sheet1.ChartObjects("Chart 1").Chart.SeriesCollection(1)
        .Points(Sheet1.Range("$A$1:$A:$100").Value).Format.Line.Visible = msoFalse
    End With

另外,除非省略,否则我会在范围内有一些#NA值导致运行时错误。

感谢您对此可行性的任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要一个迭代循环。 For/Next应该可以达到这个目的:

'Declare two range variables we will use to do this:
Dim rng as Range 'represents the entire range of ALL data cells, e.g., A1:A100
Dim r as Range   'range iterator

'First, define the range containing the values:
Set rng = Sheet1.Range("A1:A100")  'Modify as needed

'Then, begin an iteration:

For each r in rng.Cells
    If Not IsError(r.Value) and Not Trim(r.Value) = vbNullString Then
        With Sheet1.ChartObjects("Chart 1").Chart.SeriesCollection(1)
            .Points(r.Value).Format.Line.Visible = msoFalse
        End With
    ElseIf IsError r.Value Then
         'if you need to do something with the #N/A values, do it here:

    ElseIf Trim(r.Value) = vbNullString Then
         'if you need to do something with blank cells, do it here:

    End If
Next