Filtering Array Data

RudRud

Active Member
Joined
Feb 2, 2023
Messages
275
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
Hi,

Sheet1 got a list of data (estimate around 50k)

Expected Result: Filter No: 123 data into array (cArr)

I've been cleaning those datas by using Split and Join in order to get 123 data in cArr, However, its way too slow to execute and many inefficient code, Is there any more minimalism code for this kind of case?

Btw, Is that possible to filter with two criterias then store in the array as well ? Thankyou

Thank you ~

Book2
ABCDEF
1NOTESRATIOCONTROLPCTAMOUNT
2123OK3452323
333OK3451
42OK3452
52OK3455
61OK3454
72not ok3456
82not ok3457
93not ok3458
1044not ok3459
11123not ok345123
1244pending345101
1344pending34511
14123pending34558
Sheet1


VBA Code:
Sub testarr()

Dim i%
Dim aArr(), cArr()

Set found = Range("A1:aa100").Find("pct") 'This one because only want to get the datas until PCT Column


            For Each ss In Range("a2", [a2].End(xlDown))
                n = n + 1
                ReDim Preserve aArr(1 To n)
                aArr(n) = Join(Application.Transpose(Application.Transpose(ss.Resize(1, found.Column))), "#")
            Next ss


        bArr = Filter(aArr, "123")
     
       ReDim cArr(1 To UBound(bArr) + 1)
  
        For Each aR In bArr
        k = k + 1
        cArr(k) = Split(aR, "#")
  
        Next aR
      
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try the following code.
In this way you only need 2 arrays, an array with the original data, this is done to read the data from memory and not from the sheet, this makes it faster; and the second array is to put the data that meets the condition.

If you use Application.Transpose you are using a sheet function, this slows down the process, so it is preferable to read all the columns, it does not matter if there are many columns, since the reading is done in memory it is not slow.

VBA Code:
Sub testarr_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim f As Range
  
  Set f = Range("A1:aa100").Find("pct", , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    a = Range("A2", Cells(Range("A" & Rows.Count).End(3).Row, f.Column)).Value
    ReDim b(1 To UBound(a, 1), 1 To f.Column)
    
    For i = 1 To UBound(a, 1)
      If a(i, 1) = 123 Then
        k = k + 1
        For j = 1 To UBound(a, 2)
          b(k, j) = a(i, j)
        Next
      End If
    Next
  Else
    MsgBox "The text 'pct' does not exist"
  End If
End Sub

Notes:
Check if the search for "pct" is partial or the value is in the whole cell. If it is in the whole cell in the Find method change xlPart to xlWhole.

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 1
Solution
Try the following code.
In this way you only need 2 arrays, an array with the original data, this is done to read the data from memory and not from the sheet, this makes it faster; and the second array is to put the data that meets the condition.

If you use Application.Transpose you are using a sheet function, this slows down the process, so it is preferable to read all the columns, it does not matter if there are many columns, since the reading is done in memory it is not slow.

VBA Code:
Sub testarr_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim f As Range
 
  Set f = Range("A1:aa100").Find("pct", , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    a = Range("A2", Cells(Range("A" & Rows.Count).End(3).Row, f.Column)).Value
    ReDim b(1 To UBound(a, 1), 1 To f.Column)
   
    For i = 1 To UBound(a, 1)
      If a(i, 1) = 123 Then
        k = k + 1
        For j = 1 To UBound(a, 2)
          b(k, j) = a(i, j)
        Next
      End If
    Next
  Else
    MsgBox "The text 'pct' does not exist"
  End If
End Sub

Notes:
Check if the search for "pct" is partial or the value is in the whole cell. If it is in the whole cell in the Find method change xlPart to xlWhole.

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
Hi Sir, Excuse my late response.

Thank you for sharing your elegant code and being so kind to include an explanation. It means a lot more than I had previously imagined. Truly Appreacitaed

will start using this way to clean up my arrays :)
 
Upvote 1

Forum statistics

Threads
1,215,529
Messages
6,125,344
Members
449,219
Latest member
Smiqer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top