Hi
How can I to Delete a row from array , Example:
I want to delete code2 and myarray with only 2 rowsCode:myarray(0) = "code 1"
myarray(1) = "code2"
myarray(2) = "code3"
Printable View
Hi
How can I to Delete a row from array , Example:
I want to delete code2 and myarray with only 2 rowsCode:myarray(0) = "code 1"
myarray(1) = "code2"
myarray(2) = "code3"
if you are just removing the last element then you could do it like this
first declare the array dynamically
then redim it.. then u can change the size
VB Code:
Dim MyArray() As String ReDim MyArray(2) As String MyArray(0) = "code 1" MyArray(1) = "code2" MyArray(2) = "code3" ReDim Preserve MyArray(1)
Here is a very quick sample - you may turn it into a function.
I'm removing specific Index so if you don't know the index then you will first have find item you want to remove and use its index:
VB Code:
Option Explicit Dim arItems() As Long Private Sub Form_Load() Dim i& ReDim arItems(4) '5 items For i = 0 To UBound(arItems) arItems(i) = i + 1 Debug.Print arItems(i) Next i End Sub Private Sub Command1_Click() Dim i&, j& If UBound(arItems) = 0 Then ReDim arItems(0) Exit Sub End If j = 3 'remove element with Index = 3 If UBound(arItems) < j Then Exit Sub ElseIf UBound(arItems) = j Then ReDim Preserve arItems(UBound(arItems) - 1) Exit Sub End If On Error Resume Next For i = LBound(arItems) To UBound(arItems) If i >= j Then arItems(i) = arItems(i + 1) End If Next i ReDim Preserve arItems(UBound(arItems) - 1) For i = LBound(arItems) To UBound(arItems) Debug.Print arItems(i) Next i End Sub
Here's in a function:
http://www.vbforums.com/showpost.php...92&postcount=4
And in case you want to know how insert into an array:
http://www.vbforums.com/showpost.php...06&postcount=7