Closed Thread
Results 1 to 10 of 10

help! how to delete useless rows in 2 dim array by VBA

  1. #1
    Registered User
    Join Date
    12-08-2005
    Posts
    13

    help! how to delete useless rows in 2 dim array by VBA

    I just would like to fulfill autofilter function in a 2-dim array, and then delete
    useless rows in that array. Is that doable or not?

    as you know, deleting rows is so painful in worksheet even though autofilter
    is faster.

    any idea or example would be much appreciated.

    thanks

  2. #2
    Tom Ogilvy
    Guest

    Re: help! how to delete useless rows in 2 dim array by VBA

    Putting information in an array won't provide any gain if you are still
    deleting rows in the worksheet.

    You can filter so that the rows to delete are visible then

    Dim rng as Range
    set rng = activesheet.AutofilterRange.Columns(1)
    set rng = rng.offset(1,0).Resize(rng.rows.count-1)
    On Error Resume Next
    set rng1 = rng.specialcells(xlvisible)
    On Error goto 0
    if not rng1 is nothing then rng1.EntireRow.delete

    --
    Regards,
    Tom Ogilvy

    "xiang" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I just would like to fulfill autofilter function in a 2-dim array, and
    > then delete
    > useless rows in that array. Is that doable or not?
    >
    > as you know, deleting rows is so painful in worksheet even though
    > autofilter
    > is faster.
    >
    > any idea or example would be much appreciated.
    >
    > thanks
    >
    >
    > --
    > xiang
    > ------------------------------------------------------------------------
    > xiang's Profile:

    http://www.excelforum.com/member.php...o&userid=29489
    > View this thread: http://www.excelforum.com/showthread...hreadid=496567
    >




  3. #3
    Registered User
    Join Date
    12-08-2005
    Posts
    13
    Thanks for your response, Tom

    I may not describe my question clearly. What I'd like to do is
    1) putting worksheet information into array
    2)check one or more conditions for a column in array and DELETE
    those rows in which the conditions are met

    for example: A 1 2 3 4
    B 5 6 7 8
    C 9 3 2 4
    A 3 4 5 6

    result is like : B 5 6 7 8
    C 9 3 2 4
    delete those rows with "A" in the column 1
    3) write the results in array back to the worksheet to avoid deleting rows in worksheet.
    Last edited by xiang; 12-29-2005 at 04:49 PM.

  4. #4
    Registered User
    Join Date
    12-08-2005
    Posts
    13
    hi, Tom
    here is your code to remove blank entries from array
    It works perfect in one-dim array. How could I make it work for 2-dim array.
    that is something I want.

    Re: removing blank entries from an array

    --------------------------------------------------------------------------------

    j = lbound(ar) - 1
    for i = lbound(ar) to ubound(ar)
    if ar(i) <> "" then
    j = j + 1
    ar(j) = ar(i)
    end if
    Next
    Redim Preserve ar(lbound(ar) to j)

    --
    Regards,
    Tom Ogilvy

  5. #5
    Tom Ogilvy
    Guest

    Re: help! how to delete useless rows in 2 dim array by VBA

    The problem with a 2D array is that your can not redim preserve on the first
    dimension (rows). You would basically have to copy to a second array as you
    go, or another approach would be to use application.Transpose to reverse the
    row and column order.

    Sub AdjustArray()
    Dim ar As Variant, v As Variant
    Dim i As Long, j As Long, k As Long
    ReDim ar(1 To 20, 1 To 5)
    ' Build a test array with some empty elements in the first column
    For i = 1 To 20
    If Rnd() < 0.5 Then
    For j = 1 To 5
    ar(i, j) = Int(Rnd() * 100 + 1)
    Next
    End If
    Next i
    Range("A1:E20").Value = ar
    v = Application.Transpose(ar)

    j = LBound(v, 2) - 1
    For i = LBound(v, 2) To UBound(v, 2)
    If Not IsEmpty(v(1, i)) Then
    j = j + 1
    For k = LBound(v, 1) To UBound(v, 1)
    v(k, j) = v(k, i)
    Next
    End If
    Next
    ReDim Preserve v(LBound(v, 1) To UBound(v, 1), LBound(v, 2) To j)
    ar = Application.Transpose(v)
    Range("G1").Resize(UBound(ar, 1), UBound(ar, 2)).Value = ar
    Erase v
    End Sub

    For simplicity, some of the code assumes a lower bound of 1 in each
    dimension. Since your array will come from a range, this should not be a
    problem.

    --
    Regards,
    Tom Ogilvy




    "xiang" <[email protected]> wrote in
    message news:[email protected]...
    >
    > hi, Tom
    > here is your code to remove blank entries from array
    > It works perfect in one-dim array. How could I make it work for 2-dim
    > array.
    > that is something I want.
    >
    > > Re: removing blank entries from an array
    > >

    >
    > --------------------------------------------------------------------------

    ------
    > >
    > > j = lbound(ar) - 1
    > > for i = lbound(ar) to ubound(ar)
    > > if ar(i) <> "" then
    > > j = j + 1
    > > ar(j) = ar(i)
    > > end if
    > > Next
    > > Redim Preserve ar(lbound(ar) to j)
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >

    >
    >
    > --
    > xiang
    > ------------------------------------------------------------------------
    > xiang's Profile:

    http://www.excelforum.com/member.php...o&userid=29489
    > View this thread: http://www.excelforum.com/showthread...hreadid=496567
    >




  6. #6
    Registered User
    Join Date
    12-08-2005
    Posts
    13
    Hi, Tom
    thanks for your suggestion.
    But I'm afraid that Transpose approach won't work when the elements of array
    exceed 5160. BTW, I'm using excel 2000, and I don't know what will happen in execl 2003.

    Could you offer another approach?

    one more thing,
    For i = 1 To 20
    If Rnd() < 0.5 Then
    For j = 1 To 5
    ar(i, j) = Int(Rnd() * 100 + 1)
    Next
    End If
    Next i

    I don't understand above lines.
    If you can explain a little bit, that would be prefect!

  7. #7
    Tom Ogilvy
    Guest

    Re: help! how to delete useless rows in 2 dim array by VBA

    In xl2003, it isn't a problem. Here is an alternate approach that should
    work in xl97/2000

    Sub AdjustArray11()
    Dim ar As Variant, v As Variant
    Dim i As Long, j As Long, k As Long
    ReDim ar(1 To 6000, 1 To 5)
    ' Build a test array with some empty elements in the first column
    For i = 1 To 6000
    If Rnd() < 0.5 Then
    For j = 1 To 5
    ar(i, j) = Int(Rnd() * 100 + 1)
    Next
    End If
    Next i
    Range("A1:E6000").Value = ar
    j = 0
    For i = LBound(ar, 1) To UBound(ar, 1)
    If Not IsEmpty(ar(i, 1)) Then
    j = j + 1
    End If
    Next
    ReDim v(1 To j, 1 To 5)
    j = 0
    For i = LBound(ar, 1) To UBound(ar, 1)
    If Not IsEmpty(ar(i, 1)) Then
    j = j + 1
    For k = 1 To 5
    v(j, k) = ar(i, k)
    Next
    End If
    Next
    ar = v
    Range("G1").Resize(UBound(ar, 1), UBound(ar, 2)).Value = ar
    Erase v
    End Sub


    > one more thing,
    > For i = 1 To 20
    > If Rnd() < 0.5 Then
    > For j = 1 To 5
    > ar(i, j) = Int(Rnd() * 100 + 1)
    > Next
    > End If
    > Next i
    >
    > I don't understand above lines.
    > If you can explain a little bit, that would be prefect!


    Those lines just generate a test array that has blank rows in it. That is
    just for demonstration purposes.

    --
    Regards,
    Tom Ogilvy



    "xiang" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi, Tom
    > thanks for your suggestion.
    > But I'm afraid that Transpose approach won't work when the elements of
    > array
    > exceed 5160. BTW, I'm using excel 2000, and I don't know what will
    > happen in execl 2003.
    >
    > Could you offer another approach?
    >
    > one more thing,
    > For i = 1 To 20
    > If Rnd() < 0.5 Then
    > For j = 1 To 5
    > ar(i, j) = Int(Rnd() * 100 + 1)
    > Next
    > End If
    > Next i
    >
    > I don't understand above lines.
    > If you can explain a little bit, that would be prefect!
    >
    >
    > --
    > xiang
    > ------------------------------------------------------------------------
    > xiang's Profile:

    http://www.excelforum.com/member.php...o&userid=29489
    > View this thread: http://www.excelforum.com/showthread...hreadid=496567
    >




  8. #8
    Registered User
    Join Date
    10-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: help! how to delete useless rows in 2 dim array by VBA

    I was able to utilize 2 arrays to solve this problem. The first array ingests all the raw data, then clears certain rows based on a value in a cell. Once that is done, I feed that array into a new array skipping the "Blank" rows.



    Sub TestDeleteArrayRows()

    'Pull all data into first array
    Dim TestArr() As Variant
    Dim i As Long, j As Integer, k As Long, z As Long
    Sheets("DSA").Activate
    TestArr = Range(Cells(2, 1), Cells(100, 5))
    k = 0
    For i = LBound(TestArr) To UBound(TestArr)
    'Clear array row based on predetermined factor
    If TestArr(i, 4) = "10" Then
    For j = 1 To 5
    TestArr(i, j) = ""
    Next j
    Else:
    'Count non-empty rows in array
    k = k + 1
    End If
    Next i

    'New array without empty rows
    Dim ResArr()
    ReDim ResArr(1 To k, 1 To 5)
    z = 1
    For i = 1 To UBound(TestArr)
    'If the row from the first array is not empty, then bring it into the new array
    If TestArr(i, 1) <> "" Then
    For j = 1 To 5
    ResArr(z, j) = TestArr(i, j)
    Next j
    z = z + 1
    End If
    Next i


    '*****Output for testing*****
    Sheets("Test").Activate
    Range(Cells(2, 1), Cells(k + 1, 5)) = ResArr
    '*************************

    End sub

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: help! how to delete useless rows in 2 dim array by VBA

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    No need an array just using Excel basics ‼

    With an additional column to mark rows to keep or to delete aka 0 / 1 or False / True via a formula for example.
    Sort in ascending way the range on this additional colum, so the rows to delete are at the end of range.
    Clear - and not Delete ! - the block of rows to "delete" at once ! Then Clear additional column, that's it !
    See samples in threads of this forum …

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: help! how to delete useless rows in 2 dim array by VBA

    Ssniderwin,
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Ben Van Johnson

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1