subscript out of range (Excel)
long story short, i haven't used dynamic variables before and i'm not sure why i keep getting this subscript out of range error -- as soon as it gets to the row i've starred it crashes wih the error - in the debugger i can see that the value of ActiveCell is just "Trade Ticket"
VB Code:
Dim trigger As String
Dim Labels() As String
Dim Data() As Variant
Dim inputRow As Integer
Dim inputCol As Integer
Dim counter As Integer
counter = 0
inputRow = 1
inputCol = 1
Do While trigger <> "InputEnd" ' finds rows with data
If trigger = "Input" Then ' takes data out of relevant rows
inputRow = ActiveCell.Row
inputCol = ActiveCell.Column
Do While ActiveCell.Offset(0, 1) <> "End"
Label = ActiveCell.Value
**** Labels(counter) = ActiveCell.Value
Data(counter) = ActiveCell.Offset(1, 0).Value
counter = counter + 1
Loop
Else
End If
inputRow = inputRow + 1
Cells(inputRow, inputCol).Select
trigger = ActiveCell.Value
Loop
Re: subscript out of range (Excel)
Welcome to the Forums.
I dont see where your dimensioning the array Data or Labels with the number of elements needed.
Re: subscript out of range (Excel)
you need to Set then increase the amount of "space" that each variable has
Normally an array has as many spaces as its set to..
DIm tmp(3) as string
tmp has 4 spaces for data 0,1,2,3
now a dynamic array is not "setup"
DIm tmp() as string
theres no spaces.. you need to Redim to start it then increase it
Redim tmp(0)
then
Redim Preserve tmp(1) <-using preserve keeps any data in the array
VB Code:
Dim trigger As String
Dim Labels() As String
Dim Data() As Variant
Dim inputRow As Integer
Dim inputCol As Integer
Dim counter As Integer
counter = 0
inputRow = 1
inputCol = 1
Do While trigger <> "InputEnd"
If trigger = "Input" Then
inputRow = ActiveCell.Row
inputCol = ActiveCell.Column
Do While ActiveCell.Offset(0, 1) <> "End"
Label = ActiveCell.Value
[B]If counter = 0 Then
ReDim Labels(0)
ReDim Data(0)
Else
ReDim Preserve Labels(counter)
ReDim Preserve Data(counter)
End If[/B]
Labels(counter) = ActiveCell.Value
Data(counter) = ActiveCell.Offset(1, 0).Value
counter = counter + 1
Loop
Else
End If
inputRow = inputRow + 1
Cells(inputRow, inputCol).Select
trigger = ActiveCell.Value
Loop
http://www.vbforums.com/
Re: subscript out of range (Excel)
ok - i get it, i must have gotten confused when i was reading through examples, i thought Labels() would just set up a dynamic array -- for this problem it's fine to just set it up as Labels(100) and eveything seems to be working perfectly, i'll play around with the new info you gave me and see if i can get it to work both ways
thanks for the help