Random number based on certain criteria

Necroscope

Board Regular
Joined
Jul 7, 2004
Messages
72
I have a spreadsheet with a list of Invoice numbers (which all begin with INVxxxxxx) in column B and then in column C the name of the person who cleared that Invoice.

What I need to do each week is do a random check on an invoice that each person has cleared so if "John Smith" cleared 10 invoices, I want to randomly pick one of those 10 invoice numbers from the list in column B. It's sorted into date order and there are 30 different people to search through so if there is a way to help via formula or VBA, that would be a big help.

I've messed around with Lookup and Index but haven't got this working.

And if possible, I want to have a new sheet within the same file to display the results. (For example, create an "Accuracy Check" sheet and list the people and put the random invoice number next to their name so that the check can be carried out.)

Can anyone help?

TIA.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Here's one possibility.
Assumes that the dates are in column A of the source sheet.

Code:
Sub ft()
With Sheets("Accuracy Check")
    .Cells.ClearContents
    Sheets("Sheet1").[A:C].Copy .[A:A] 'Change sheet name required
    .Range([D2], Cells(Rows.Count, 3).End(xlUp)(1, 2)).Formula = "=RAND()"
    .[A:D].Sort Key1:=[D1], Header:=xlYes
    .[D:D].Delete
    .[A:C].RemoveDuplicates Columns:=3, Header:=xlYes
    .[A:C].Sort Key1:=[A1], Header:=xlYes
End With
End Sub
 
Upvote 0
If you want a non VBA solution

Sheet1
BC
1invoicecleared by
2INV000001Rick
3INV000002Bob
4INV000003Mike
5INV000004Bert
6INV000005Rick
7INV000006Mike
8INV000007Rick
9INV000008Mike
10INV000009Mike
11INV000010Rick
12INV000011Bob
13INV000012Bert
14INV000013Rick
15INV000014Rick
16INV000015Bert
17INV000016Rick
18INV000017Rick
19INV000018Rick
20INV000019Rick

<tbody>
</tbody>


Sheet 2
AB
1NameInv to check
2RickINV000007
3BobINV000011
4MikeINV000008
5BertINV000012

<colgroup><col><col></colgroup><tbody>
</tbody>

In B2 of sheet 2
This is an array formula and must be committed with CONTROL+SHIFT+ENTER. If done correctly Excel will Put {} around the formula. Copy down
Code:
=INDEX(Sheet1!$B$2:$B$20,SMALL(IF(Sheet1!$C$2:$C$20=A2,ROW(Sheet1!$C$2:$C$20)-ROW(Sheet1!$C$2)+1),RANDBETWEEN(1,COUNTIF(Sheet1!$C$2:$C$20,A2))))
 
Upvote 0
Hi footoo. Thanks for that - it works great.

Another thought had just occurred to me (apologies for not thinking of this first time round). The 30 people in question each belong to Team 1, Team 2 or Team 3. This info is in column E of the Invoice sheet (Sheet1). Would it be possible to add to your existing code to either list all the team 1, 2 and 3 people together OR even just only list team 1 people?
 
Upvote 0
Hi Scott. This also works great for me so thank you for taking the time to reply and help me out. It's much appreciated.
 
Upvote 0
Another thought had just occurred to me (apologies for not thinking of this first time round). The 30 people in question each belong to Team 1, Team 2 or Team 3. This info is in column E of the Invoice sheet (Sheet1). Would it be possible to add to your existing code to either list all the team 1, 2 and 3 people together OR even just only list team 1 people?
Code:
Sub ft2()
Application.ScreenUpdating = False
With Sheets("Accuracy Check")
    .Cells.ClearContents
    Sheets("Sheet1").[A:C].Copy .[A:A]
    Sheets("Sheet1").[E:E].Copy .[D:D]
    .Range([E2], Cells(Rows.Count, 3).End(xlUp)(1, 3)).Formula = "=RAND()"
    .[A:E].Sort Key1:=[E1], Header:=xlYes
    .[E:E].Delete
    .[A:D].RemoveDuplicates Columns:=3, Header:=xlYes
    .[A:D].Sort Key1:=[D1], Key2:=[A1], Header:=xlYes
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,535
Members
449,169
Latest member
mm424

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