Sampling of data using formula or macros

Sailadarohit

New Member
Joined
Sep 7, 2022
Messages
39
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Book1
ABC
1U.ID(Not Unique)Main ID (Unique)Priority
2110High
3111Medium
4212Medium
5313Low
6514High
7415Low
8716Medium
9517High
10718Medium
11119Medium
12220Low
13821High
14922Low
15623Medium
16524High
17425Medium
18226Medium
19827Low
20128High
21629Low
22930Medium
23331High
24632Medium
25933Medium
26134Low
27135High
28236Low
29337Medium
30538High
31439Medium
32740Medium
33541Low
34742High
35143Low
36244Medium
37845High
38946Medium
39647Medium
40548Low
41449High
42250Low
43851Medium
44152High
45653Medium
46954Medium
47355Low
48656High
49957Low
Sheet1


There is a use case where we have U.ID which is not unique, main ID which is unique and priority which covers high, medium and low. In a general scenerio we have mutiple Main ID's associated with one U.ID . For audit purpose we require a sample of this data which will have only 3 main ID's associated with each U.ID.
Please note in real scenario this is a huge data set so manually doing this is not possible.
Can anyone help with an excel formula (preferred) or a macro
 
How about
VBA Code:
Sub sailadarohit()
   Dim Rng As Range
   Dim Ary As Variant, Nary As Variant
   Dim nr As Long, r As Long
  
   Set Rng = Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row)
   Ary = Evaluate("sortby(" & Rng.Address & ",randarray(" & Rng.Rows.Count & "))")
   ReDim Nary(1 To UBound(Ary), 1 To 3)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If .Item(Ary(r, 1)) < 3 Then
            .Item(Ary(r, 1)) = .Item(Ary(r, 1)) + 1
            nr = nr + 1
            Nary(nr, 1) = Ary(r, 1)
            Nary(nr, 2) = Ary(r, 2)
            Nary(nr, 3) = Ary(r, 3)
         End If
      Next r
   End With
   Nary = Application.Sort(Nary)
   Range("E2").Resize(nr, 3).Value = Nary
End Sub
EDIT
Forgot to mention, this will only work with 2021 or 365
 
Last edited:
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about
VBA Code:
Sub sailadarohit()
   Dim Rng As Range
   Dim Ary As Variant, Nary As Variant
   Dim nr As Long, r As Long
 
   Set Rng = Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row)
   Ary = Evaluate("sortby(" & Rng.Address & ",randarray(" & Rng.Rows.Count & "))")
   ReDim Nary(1 To UBound(Ary), 1 To 3)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If .Item(Ary(r, 1)) < 3 Then
            .Item(Ary(r, 1)) = .Item(Ary(r, 1)) + 1
            nr = nr + 1
            Nary(nr, 1) = Ary(r, 1)
            Nary(nr, 2) = Ary(r, 2)
            Nary(nr, 3) = Ary(r, 3)
         End If
      Next r
   End With
   Nary = Application.Sort(Nary)
   Range("E2").Resize(nr, 3).Value = Nary
End Sub
EDIT
Forgot to mention, this will only work with 2021 or 365
Thanks fluff for taking out time and helping me with this, i have 2 questions:
1. If my range changes what should i change in the code
2. If the sample size number changes from 3 per U.ID to a different number what should i change in the code.

TIA
 
Upvote 0
If the sample size changes then change the 3 on this line
VBA Code:
If .Item(Ary(r, 1)) < 3 Then
The code uses the last used row in col A to pick-up all the data.
 
Upvote 0
If the sample size changes then change the 3 on this line
VBA Code:
If .Item(Ary(r, 1)) < 3 Then
The code uses the last used row in col A to pick-up all the data.
for the first part if i have my data from Row A to Row F i should change it as shown below. Correct?
Set Rng = Range("A2:F" & Range("A" & Rows.Count).End(xlUp).Row)
 
Upvote 0
for the first part if i have my data from Row A to Row F i should change it as shown below. Correct?
No, that will still only return the 1st 3 columns.
If your data is not like what you posted, then post something that is correct.
 
Upvote 0
@Fluff - just for my learning here - when you do this part:
VBA Code:
 If .Item(Ary(r, 1)) < 3 Then
            .Item(Ary(r, 1)) = .Item(Ary(r, 1)) + 1

I can see it is creating a dictionary of each unique U.ID from Col A. I'm not following how it distinguishes between the actual "item", and the "running count of the items" and where they are stored ?
Can you elaborate a little by chance please, as it look pretty genius.

thanks
Rob
 
Upvote 0
thanks - indeed that site is my goto on Dictionaries .. and I've poured over it for ages trying to get my head around it.

I guess I was a little fried on the fact that its both creating items, and counting items at the same time. Makes sense now - cheers
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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