VBA to create list of unique values matching a condition, drawn from a large dataset

Frysk

New Member
Joined
Oct 13, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi all, and thanks for looking.

I have a dataset of 500,000+ rows.

Column A = unique identifier (alphanumeric), with many duplicates. For example, "Abby1" might have 1,000 rows before we see "Adam1".
Column B = helper column, filled down, where a "1" indicates if this row is relevant. Otherwise it is a "0".

Maybe relevant: "Abby1" rows will always be together. Once they end and "Adam1" rows begin, "Adam1" rows are together, until the next unique identifier, and so on.

Goal: to create a list of unique values (from column A), where those values each have at least one instance of "1" in their rows. Example, if Abby has 1,000 rows, and at least one row has a "1" in helper column B, then Abby is included in this unique list.

I have tried a few functions that would work if the dataset were smaller, but the workbook either crashes / not enough memory, or calculates for too long a time. I'm guessing the solution is some clever VBA code to populate a new column with the list of unique values. If anyone is able to help with this code, I would be grateful.

Thank you again.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Does this do what you want? (Unique values put in column D - change destination to suit)

VBA Code:
Option Explicit
Sub Frysk()
    Dim d As Object, arr, i As Long
    Set d = CreateObject("scripting.dictionary")
    arr = Range("A1", Cells(Rows.Count, "B").End(xlUp))
    For i = 1 To UBound(arr, 1)
        If arr(i, 2) = 1 Then d(arr(i, 1)) = 1
    Next i
    Range("D1").Resize(d.Count, 1).Value2 = Application.Transpose(d.keys)
End Sub
 
Upvote 0
Solution
Does this do what you want? (Unique values put in column D - change destination to suit)

VBA Code:
Option Explicit
Sub Frysk()
    Dim d As Object, arr, i As Long
    Set d = CreateObject("scripting.dictionary")
    arr = Range("A1", Cells(Rows.Count, "B").End(xlUp))
    For i = 1 To UBound(arr, 1)
        If arr(i, 2) = 1 Then d(arr(i, 1)) = 1
    Next i
    Range("D1").Resize(d.Count, 1).Value2 = Application.Transpose(d.keys)
End Sub

Brilliant, thank you - it's great.
 
Upvote 0
@kevin9999
The Transpose function has a limit of 65,536 items. Beyond this limit, it only retrieves the remaining items, unfortunately without raising an error.
Try this:
VBA Code:
Sub test_Transpose_limit()
vb = Application.Transpose(Range("A1:A100000"))
Debug.Print UBound(vb)  'returns: 34464,  it's 100000 - 65536
End Sub

Therefore, it's advisable to include a check for this in your code, in case the result, i.e d.keys, surpasses that limit.
 
Upvote 0
@kevin9999
The Transpose function has a limit of 65,536 items. Beyond this limit, it only retrieves the remaining items, unfortunately without raising an error.
Try this:
VBA Code:
Sub test_Transpose_limit()
vb = Application.Transpose(Range("A1:A100000"))
Debug.Print UBound(vb)  'returns: 34464,  it's 100000 - 65536
End Sub

Therefore, it's advisable to include a check for this in your code, in case the result, i.e d.keys, surpasses that limit.
Thanks for the reminder @Akuini - I forgot all about that
 
Upvote 0
I know this is showing as solved, however a simple formula might do the exact same thing, especially if the data is sorted. in A. If It starts in Row A2 (Assuming you have a heading), then in B2 add the following formula then filter by the identifier of 1
Excel Formula:
=IF(A2=A1,0,1)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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