[VBA] adding a band to a line-chart programmatically

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
393
Office Version
  1. 2021
Platform
  1. Windows

Excel 2012
ABCDEFGHIJKLM
1Using 2 additional helper rows to set band
2JanFebMarAprMayJunJulAugSepOctNovDec
3Inventory7153582930781768271441
4Lower Band Value606060606060606060606060
5Upper Band Value707070707070707070707070
6
7
8
9Without using 2 additional helper rows
10JanFebMarAprMayJunJulAugSepOctNovDec
11Inventory7153582930781768271441
工作表2



Is it possible to add colored band ( on value axis )to a line chart via VBA, such that no additional helper rows are needed for adjustment ( the table below )?
i.e. user simply runs the code and edit the band width within the VBE.


Thanks a lot!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi

See if this helps.

Code:
' add band to line chart
Sub AddBand()
Dim ws As Worksheet
Dim cht As Chart
Dim dArray() As Double
Dim lMin As Long, lMax As Long
Dim lPoints As Long, j As Long

lMin = 3 ' minimum band value
lMax = 5 ' maximum band value

Set ws = Worksheets("Sheet3") ' worksheet where the chart is located
Set cht = ws.ChartObjects(1).Chart ' first chart in worksheet

' adds the 2 series for the band
With cht

    ReDim dArray(1 To .SeriesCollection(1).Points.Count)
    
    ' add the second series (below band)
    
    With .SeriesCollection.NewSeries
        .ChartType = xlColumnStacked
        .Name = "Series 2"
        For j = 1 To UBound(dArray): dArray(j) = lMin: Next j
        .Values = dArray
        
        ' make it invisible
        .Format.Fill.Visible = msoFalse
        .Format.Line.Visible = msoFalse
    End With

    ' add the third series (between Min and Max of the band)
    
    With .SeriesCollection.NewSeries
        .ChartType = xlColumnStacked
        .Name = "Series 3"
        For j = 1 To UBound(dArray): dArray(j) = lMax - lMin: Next j
        .Values = dArray
    End With

    ' no gap so that is a band
    .ChartGroups(1).GapWidth = 0


End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top