Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a relatively big amount of data which I want to put in graphs.

Basically, what I have is multiple components (in total 30-ish but in the example below I'll limit the code to 5 components), and each component has one column. I've assigned the column numbers to the respective components, declared as integers. Sometimes I don't want a component to have a graph so that's where there's a gap in the numbers (e.g. in between diat and hydr is another component in column 5 but for this one there shouldn't be a graph).

Then I wanted to put all the components of which I want a graph into one array and do a For... Next loop so a graph would be automatically created for each element in the array (so for each component in the array).

Obviously I did something wrong :-). The code gets stuck on the first time I try to refer to the respected element in the array: ActiveChart.SeriesCollection(1).Name = Cells(9, componentlist(1, i))

Dim diat, hydr, para, terb, theo As Integer
diat = 4        'column number of the component named diat
hydr = 6        'column number of the component named hydr
para = 7        'column number of the component named para
terb = 9        'column number of the component named terb
theo = 10       'column number of the component named theo

Dim componentlist As Variant
componentlist = Array(diat, hydr, para, terb, theo)

For i = 1 To UBound(componentlist)

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
ActiveChart.SeriesCollection.NewSeries.Select
ActiveChart.SeriesCollection(1).Name = Cells(9, componentlist(1, i))
ActiveChart.SeriesCollection(1).XValues = Worksheets(2).Range(Cells(10, 3), Cells(21, 3))
ActiveChart.SeriesCollection(1).Values = Worksheets(2).Range(Cells(10, componentlist(1, i)), Cells(21, componentlist(1, i)))

Next

My experience with VBA is limited so does anyone of you know how to solve this? Thanks in advance!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Array() creates a single-dimension array with lower bound of zero (unless you're using Option Base 1 at the top of your module).

So you should loop from 0 to ubound(componentlist)-1 and you only need to use componentlist(i) since your array only has one dimension.

share|improve this answer
    
Thanks! Worked :) –  user3042961 Jan 29 at 21:17

The first thing you should investigate is whether LBOUND(componentlist) is 1 or 0 and fix the for loop if necessary.

The next thing to fix is the dimensionality of componentlist . It should be single-dimensioned.

share|improve this answer
    
Same answer as above, works :-). Cheers! –  user3042961 Jan 29 at 21:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.