简体   繁体   中英

Loop Chart Changes for Range of Specific Worksheet Values

The following code deletes a line segment in a chart. I would like to run it repeatedly for a row of values versus this single value of $A$1:

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

While this is incorrect syntax, the general goal might looks something like this:

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

Also, I have some #NA values in the range that will result in runtime errors unless omitted.

Thank you for any assistance on the feasibility of this!

You need an iterative loop. For/Next should work for this purpose:

'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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM