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.

Pretty new to VBA but I'm learning quickly.

I'm finally getting used to using loops to perform repetitive tasks, and in this case, I want each pass through the loop to define a different variable. I'd be defining a list as a string and pushing a value to each part of the list for each loop.

Obviously, the number of loops is variable, so I need the end point of the defined list to be a variable. From what I've searched, that's not possible? I'm getting a "constant expression required" error.

Here is the code (lastRow is already defined):

NextAverage = 0
section = 1
Dim AverageSection(1 To section) As String
For section = 1 To PhraseSections

ActiveCell.Formula = "=MATCH(""average"",A" & NextAverage + 1 & ":A" & lastRow & ",0)"
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=SUM(G1:G" & section & ")"
NextAverage = ActiveCell.Value
AverageSection(section) = ActiveCell.Value

Next section

Any help would be greatly appreciated!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Try using Redim Preserve:

Dim AverageSection() As String
For section = 1 To PhraseSections
    Redim Preserve AverageSection(section)
    ...
Next section

Does this help?

share|improve this answer
    
@cricialfix So simple! I'll look into what that means--hopefully it's not too over my head. Thanks very much! –  LeeLK May 21 '13 at 18:07

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.