Home » excel » vba – How to Use Every nth Cell in a Chart in Excel Programmatically

vba – How to Use Every nth Cell in a Chart in Excel Programmatically

Posted by: admin April 23, 2020 Leave a comment


An input file has about 500,000 rows. The main thing I am trying to do is to plot only 10,000 of the 500,000 cells over the whole range of cells. Is there a way to use every 50th cell in the chart programmatically? If not, is the best approach to copy every 50th cell to a new location, and then plot?

How to&Answers:

For a relatively small amout of data, it is possible to set the x and y – Data to a list of separate cells.

This is best done programatically in VBA. The Spreadsheet was renamed to ‘A’ to keep the Data-String as small as possible. The values for to and step in the loop may be varied:

Dim xS As String
Dim yS As String
xS = "="
yS = "="

For i = 1 To 23000 step 50
    If i > 1 Then
        xS = xS & ","
        yS = yS & ","
    End If
    xS = xS & "A!$A$" & CStr(i)
    yS = yS & "A!$B$" & CStr(i)

ActiveChart.FullSeriesCollection(1).XValues = xS
ActiveChart.FullSeriesCollection(1).Values = yS

I am not sure, however, what the maximul length of the XValues String is. First tests showed at least 4032 characters. This would bring you to an amout of about 350 to 450 values per Chart-SeriesCollection. If You add about 30 series-collection, this might be a solution to hold your 10000 value-pairs.
This is a bit cumbersome and only worth it, if your date changes.

If You aim to plot a static list of numbers it is best to copy cells programmatically to a second spreadsheet and then plot them:

for i = 1 to 500000 step 50
   destinationSheet.range(1,i/1000).value = sourceSheet.range(1,i).value


If you don’t want to go down the VBA route, you could utilise the OFFSET function in another sheet to pull out x rows from a specified target and copy down further. You might need a helper column to achieve this..

For example, I might have something like =OFFSET($A$1,B1, 0) where values in column B increase in increments of 50…this might be a different way of looking at it. You could then just hook your chart up to this data.