Results 1 to 4 of 4

Thread: to delete a row from array

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    to delete a row from array

    Hi

    How can I to Delete a row from array , Example:

    Code:
      myarray(0) = "code 1"
      myarray(1) = "code2"
      myarray(2) = "code3"
    I want to delete code2 and myarray with only 2 rows

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: to delete a row from array

    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:
    1. Dim MyArray() As String
    2.     ReDim MyArray(2) As String
    3.    
    4.     MyArray(0) = "code 1"
    5.     MyArray(1) = "code2"
    6.     MyArray(2) = "code3"
    7.    
    8.     ReDim Preserve MyArray(1)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: to delete a row from array

    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:
    1. Option Explicit
    2.  
    3. Dim arItems() As Long
    4.  
    5. Private Sub Form_Load()
    6. Dim i&
    7.  
    8.     ReDim arItems(4) '5 items
    9.     For i = 0 To UBound(arItems)
    10.         arItems(i) = i + 1
    11.         Debug.Print arItems(i)
    12.     Next i
    13.    
    14. End Sub
    15.  
    16. Private Sub Command1_Click()
    17. Dim i&, j&
    18.  
    19.     If UBound(arItems) = 0 Then
    20.         ReDim arItems(0)
    21.         Exit Sub
    22.     End If
    23.    
    24.     j = 3 'remove element with Index = 3
    25.    
    26.     If UBound(arItems) < j Then
    27.         Exit Sub
    28.     ElseIf UBound(arItems) = j Then
    29.         ReDim Preserve arItems(UBound(arItems) - 1)
    30.         Exit Sub
    31.     End If
    32.    
    33.     On Error Resume Next
    34.    
    35.     For i = LBound(arItems) To UBound(arItems)
    36.         If i >= j Then
    37.             arItems(i) = arItems(i + 1)
    38.         End If
    39.     Next i
    40.    
    41.     ReDim Preserve arItems(UBound(arItems) - 1)
    42.    
    43.     For i = LBound(arItems) To UBound(arItems)
    44.         Debug.Print arItems(i)
    45.     Next i
    46.  
    47. End Sub

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: to delete a row from array

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width