PDA

View Full Version : Filling Arrays



Giri
05-19-2012, 06:03 AM
Hi Guys,

I am getting a Run-Time Error 9 (Subscript out of Range) when trying to fill an array in the following code.


Dim u As UnitAdjInfo
ReDim unitadj_value(1000)
ReDim fundName(1000)
Dim x As Integer
x = 0

Do
' The LastWord function returns the last word in a string
u.fundName(x) = LastWord(foundCell.Value) ' Run-Time Error 9 Subscript out of Range Error
u.unitadj_value(x) = foundCell.Offset(0, -7).Value



Set foundCell = Range("N2:N" & lastrow_Noms).FindNext(foundCell)
x = x + 1

Loop While Not foundCell Is Nothing And foundCell.Address <> firstfound_Address


In another module, I have the following:



Type UnitAdjInfo

unitadj_value() As Double

fundName() As String

End Type




Anyone know what I am doing wrong here?

Thank you for your help!

Giri

Paul_Hossler
05-19-2012, 06:55 AM
The way I've always seem arrays of UDT's handled is like this.

Doesn't mean that there aren't other ways, but FWIW this is the way I've usually seen them



Option Explicit

Type UnitAdjInfo

unitadj_value As Double
fundName As String

End Type

Sub test()
Dim UnitAdj(0 To 1000) As UnitAdjInfo

Dim x As Long

x = 0

'assuming that foundCell in not Nothing the first time in .....
Do
UnitAdj(x).fundName = "test" ' LastWord(foundCell.Value)
UnitAdj(x).unitadj_value = 123 ' foundCell.Offset(0, -7).Value

' Set foundCell = Range("N2:N" & lastrow_Noms).FindNext(foundCell)
x = x + 1
Loop While x < UBound(UnitAdj)
' Loop While Not foundCell Is Nothing And foundCell.Address <> firstfound_Address
Stop
End Sub


Paul

Giri
05-21-2012, 03:58 AM
Thanks so much for that Paul!

Kind Regards,

Giri