A
Al
Hey folks,
The following code relates to a listbox which is populated from an array
that has 9 rows and as many columns as items in the listbox.
When a user selected and item in the listbox and hits delete it also needs
to be deleted from the corresponding array. The following codes moves the
selected array column to the last element so it can be deleted in a Re Dim
preserve. (It has to be a column because you can only delete the last
element.)
For some reason if their is only one row in the list box, and hence one
column in the array I get a
Subscript out of range error (runtime error 9)
The codes breaks on line and again on the second instance of the line.
ReDim Preserve OffenderArray(9, NameCount - 2) 'This will flush the last
record
I guess its something to do with trying to delete the only row in the array?
The full code is below if anyone can help. Thanks!!
-Al
Sub DeleteColumnOffenderArray()
NameCount = ListBox2.ListCount ' Counts names
NametoDelete = ListBox2.ListIndex ' Selected Name
'the array size is OffenderArray(NameCount - 1,9)
'redefine the array
If NametoDelete = NameCount - 1 Then 'This means the user wants
to delete the last record
ReDim Preserve OffenderArray(9, NameCount - 2) 'This will
flush the last record
ListBox2.Column() = OffenderArray ' This updates listbox2
from new array
Else
For i = 0 To NameCount - NametoDelete - 2
For j = 0 To 9
OffenderArray(j, NametoDelete + i) =
OffenderArray(j, NametoDelete + i + 1)
Next j
Next i
End If
ReDim Preserve OffenderArray(9, NameCount - 2) ' Deletes Last Column in
Array
ListBox2.Column() = OffenderArray ' This updates listbox2 from new array
End Sub
The following code relates to a listbox which is populated from an array
that has 9 rows and as many columns as items in the listbox.
When a user selected and item in the listbox and hits delete it also needs
to be deleted from the corresponding array. The following codes moves the
selected array column to the last element so it can be deleted in a Re Dim
preserve. (It has to be a column because you can only delete the last
element.)
For some reason if their is only one row in the list box, and hence one
column in the array I get a
Subscript out of range error (runtime error 9)
The codes breaks on line and again on the second instance of the line.
ReDim Preserve OffenderArray(9, NameCount - 2) 'This will flush the last
record
I guess its something to do with trying to delete the only row in the array?
The full code is below if anyone can help. Thanks!!
-Al
Sub DeleteColumnOffenderArray()
NameCount = ListBox2.ListCount ' Counts names
NametoDelete = ListBox2.ListIndex ' Selected Name
'the array size is OffenderArray(NameCount - 1,9)
'redefine the array
If NametoDelete = NameCount - 1 Then 'This means the user wants
to delete the last record
ReDim Preserve OffenderArray(9, NameCount - 2) 'This will
flush the last record
ListBox2.Column() = OffenderArray ' This updates listbox2
from new array
Else
For i = 0 To NameCount - NametoDelete - 2
For j = 0 To 9
OffenderArray(j, NametoDelete + i) =
OffenderArray(j, NametoDelete + i + 1)
Next j
Next i
End If
ReDim Preserve OffenderArray(9, NameCount - 2) ' Deletes Last Column in
Array
ListBox2.Column() = OffenderArray ' This updates listbox2 from new array
End Sub