How to pass a value multiple times in a Chart using VBA

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I want to pass a computed value n number of times in a Chart. For example, in an already plotted chart with 36 values, I want to add another series (say, mean score) to the Chart. I tired to put a macro but it is not giving the output.
VBA Code:
Sub Chrt1()
   
    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Values = _
        "={50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50}"

End Sub
Sub Chrt2()

    Dim u$
    u=Application.WorksheetFunction.Rept("50",36)

    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Values = _
        "={" & u & "}"""

End Sub

I want to achieve Chrt1 output using Chrt2 logic.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try building your string as follows...

VBA Code:
    Dim u$
    u = Application.WorksheetFunction.Rept(",50", 36)
    u = Mid(u, 2)

Then assign it as follows...


VBA Code:
ActiveChart.SeriesCollection(2).Values = "={" & u & "}"

So your code would be amended as follows...

VBA Code:
Sub Chrt2()

    Dim u$
    u = Application.WorksheetFunction.Rept(",50", 36)
    u = Mid(u, 2)

    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Values = "={" & u & "}"

End Sub

Your code could also be re-written as follows...

VBA Code:
Sub Chrt3()

    Dim u$
    u = Application.WorksheetFunction.Rept(",50", 36)
    u = Mid(u, 2)

    With ActiveChart.SeriesCollection.NewSeries
        .Values = "={" & u & "}"
    End With

End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,215,256
Messages
6,123,914
Members
449,132
Latest member
Rosie14

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