Search code examples
vbaexcelexcel-charts

Auto Adjust Excel Chart Line Weights With a Loop


I am trying to adjust the line thickness (or weights) on an Excel chart. I want each line to adjust in thickness based on a weight I have saved in a named range "Weights"

I am having trouble getting this double loop right

I have the vollowing VBA loop, but at present it adjusts each chart serries to all every value in my "Weights" series, then carries on looping to the next Srs. I want each value in my Srs to change only once, then select the next weight.

When I try to force the loop onto the next "Srs" I get the 'Invalid next control variable reference'

How does this double loop need to be constructed to adjust each line (Srs) to each of my weights (£w)

Thanks

Sub SetWeights()
    Dim Srs As Series
    Dim myWeight As Range
    Dim £w As Range
    
    Set myWeight = Range("Weights")
 
    
    With ActiveSheet
        For Each Srs In ActiveChart.SeriesCollection
             For Each £w In myWeight
             Srs.Format.Line.Weight = £w
      'Debug.Print £w
    Next 'Srs
        
        
    Next '£w
        
    End With

End Sub


Solution

  • Is this what you are trying?

    Sub SetWeights()
        Dim Srs As Series
        Dim myWeight As Range
        Dim £w As Range
        Dim j As Long
    
        Set myWeight = Range("Weights")
    
        j = 1
    
        With ActiveSheet
            For Each £w In myWeight
                If j > ActiveChart.SeriesCollection.Count Then Exit Sub
                ActiveChart.SeriesCollection(j).Format.Line.Weight = £w
                j = j + 1
            Next £w
        End With
    End Sub
    

    enter image description here