Hello everyone,
I'm new to this forum, came to here because I have a problem with some code and it giving me a headache. Most frustrating part is probably because it's about something basic as defining an array and populating it. I'm just doing something wrong but can't see it.
Below is the code with explenation
At the start of the code, I define 2 dynamical arrays (no problem here). In the first array I store client information and in the second I store some figures relevant to the client.
Public ExpRotAllClient() As String
Public ExpRotAll() As Long
Because I don't want the array to be empty, I fill in some data in the first line of both arrays. (Still no problem)
ReDim ExpRotAllClient(1, 2)
ExpRotAllClient(1, 1) = "Unknown"
ExpRotAllClient(1, 2) = "BEANR"
ReDim ExpRotAll(1, 4)
ExpRotAll(1, 1) = 0
ExpRotAll(1, 2) = 0
ExpRotAll(1, 3) = 0
ExpRotAll(1, 4) = 0
Now the part that give me a headache. The program I created will loop through a long list of data. Each line in the list, I have to check if client data (mentioned in 2 different columns) is already added to the array. If it is, I store the location in the array for fast access or add the client information to the array. The code below is the part where I compare data from the list with data in the array.
'Check in rotation array if combination client & plor exist and if needed create it
'Also store the location in the array where client can be found so we don't have to search array constantly to add data
ClientExists = 0
RotationArrayCount = UBound(ExpRotAllClient, 1)
MsgBox (RotationArrayCount)
For I = 1 To RotationArrayCount
If ExpRotAllClient(I, 1) = Worksheets("Data").Range("M" & Currentrow) And ExpRotAllClient(I, 2) = Worksheets("Data").Range("N" & Currentrow) Then
ClientExists = 1
ClientArrayLocation = I
End If
Next
If ClientExists = 0 Then
RotationArrayCount = RotationArrayCount + 1
MsgBox (RotationArrayCount)
ReDim Preserve ExpRotAllClient(RotationArrayCount, 2)
ExpRotAllClient(RotationArrayCount, 1) = Worksheets("Data").Range("M" & Currentrow)
ExpRotAllClient(RotationArrayCount, 2) = Worksheets("Data").Range("N" & Currentrow)
ReDim Preserve ExpRotAll(RotationArrayCount,4)
ExpRotAll(RotationArrayCount, 1) = 0
ExpRotAll(RotationArrayCount, 2) = 0
ExpRotAll(RotationArrayCount, 3) = 0
ExpRotAll(RotationArrayCount, 4) = 0
ClientArrayLocation = RotationArrayCount
End If
When running the script, it stops on the line I marked in bold and gives Runtime error 9, Subscript out of range error. But I suspect the cause of this error is in the line below. I've added the MsgBox line code to see if RotationArrayCount contains wrong information what would cause the error, but it's not the case. First time it contains 1 and second time it contains 2 and that is what it should be.
I hope you guys see what I fail to see.
Bookmarks