VBA How to auto populate number randomly with criteria that each person must be picked once

AmirFirdaus9509

New Member
Joined
Feb 14, 2022
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Hi , I Am currently creating an Audit Tool for Excel.
I have create a VBA where the function will calculate the total number based on each name and its respective class from a raw data in Tab "Raw Data" and populate them into Table Below .However , To do the audit , i would ask the auditor to add up the number themselves in column K till Q but this is not a fair way to segregate the audit. I would like the excel to randomly add it but with criteria that atleast 1 name should be picked at all times.

Is there a way in Column K6 to Q6 to down below to randomly add up to the number based on green cell on Column D3 to J3?


1719477241892.png


Thanks and greatly appreciated on the help
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I couple more questions/confirmations if I may.
1) What is likely max number of rows, ie names listed?
1) Each name will have at least one AWB count value in D:J ?
2) Each name is required to have at least one audit?
3) Other than condition 2), the selection is random?
4) If column entries >= to audits required (green) then only one audit per name for that column?
The above I believe I can achieve.

Where it gets a bit more difficult, for me, is....
4) If number of audits required (Green) exceeds number of entries in it's column, then multiple audits are allocated?
Hopefully that makes sense?
 
Upvote 0
Ok, maybe best to run this by you. Below is my test data and the code that creating the random selection, albeit that they are all 1's.
Maybe copy it and test to see if I am on the track?

AutoTraining.xlsm
ABCDEFGHIJKLMNOPQ
1
2
331172421
4
5N0Name
61NM121
72NM28131611
83NM31534711
94NM4321
105NM526173111
116NM6641
127NM7601
138NM8591
149NM9221
1510NM1031
1611NM1111
1712NM1221
1813
1914
2015
2116
2217
2318
2419
2520
26
Sheet8


VBA Code:
Sub RandAudit()

Dim lr As Long, i As Long
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range

lr = Range("C" & Rows.Count).End(xlUp).Row
Set Rng1 = Range("K6:Q" & lr)
Set Rng2 = Range("S6:X" & lr)
Set Rng3 = Range("S6:S" & lr)

Application.ScreenUpdating = False
Application.EnableEvents = False
For i = 1 To 200
      Rng2.Formula = "=IF(D6>0,RAND(),"""")"
       Rng2.Value = Rng2.Value
    Rng1.Formula = "=IFERROR(IF(RANK(S6,S$6:S$" & lr & ",0)<=D$3,1,""""),"""")"
    Rng1.Value = Rng1.Value
    Rng2.ClearContents
    
    Rng3.Formula = "=SUM(K6:Q6)>0"
    If Application.WorksheetFunction.CountIf(Rng3, True) = lr - 5 Then Exit For

Next
Rng3.ClearContents
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub
 
Upvote 0
Hi , sorry for my belated reply as it was night time in my zone

To answer your question .

1. At max , will be 15 names at all times.
2. Yes , If they are working on that day , atleast one AWB will be shown up
3. Correct , the previous version is totally random that may not picked the user name.
4. Yes , I would like the excel to choose it for the auditor. Yes , it is expect to exceed but i would like for atleast all user to be picked.
I couple more questions/confirmations if I may.
1) What is likely max number of rows, ie names listed?
1) Each name will have at least one AWB count value in D:J ?
2) Each name is required to have at least one audit?
3) Other than condition 2), the selection is random?
4) If column entries >= to audits required (green) then only one audit per name for that column?
The above I believe I can achieve.

Where it gets a bit more difficult, for me, is....
4) If number of audits required (Green) exceeds number of entries in it's column, then multiple audits are allocated?
Hopefully that makes sense
 
Upvote 0
Ok, maybe best to run this by you. Below is my test data and the code that creating the random selection, albeit that they are all 1's.
Maybe copy it and test to see if I am on the track?

AutoTraining.xlsm
ABCDEFGHIJKLMNOPQ
1
2
331172421
4
5N0Name
61NM121
72NM28131611
83NM31534711
94NM4321
105NM526173111
116NM6641
127NM7601
138NM8591
149NM9221
1510NM1031
1611NM1111
1712NM1221
1813
1914
2015
2116
2217
2318
2419
2520
26
Sheet8


VBA Code:
Sub RandAudit()

Dim lr As Long, i As Long
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range

lr = Range("C" & Rows.Count).End(xlUp).Row
Set Rng1 = Range("K6:Q" & lr)
Set Rng2 = Range("S6:X" & lr)
Set Rng3 = Range("S6:S" & lr)

Application.ScreenUpdating = False
Application.EnableEvents = False
For i = 1 To 200
      Rng2.Formula = "=IF(D6>0,RAND(),"""")"
       Rng2.Value = Rng2.Value
    Rng1.Formula = "=IFERROR(IF(RANK(S6,S$6:S$" & lr & ",0)<=D$3,1,""""),"""")"
    Rng1.Value = Rng1.Value
    Rng2.ClearContents
  
    Rng3.Formula = "=SUM(K6:Q6)>0"
    If Application.WorksheetFunction.CountIf(Rng3, True) = lr - 5 Then Exit For

Next
Rng3.ClearContents
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub
I tested your sample code and it is on the right track in randomize the selection when run multiple times,
Is there a way to add up the number to 11 instead of 1's , it will provide an add up lets just say 5+4+2?
 
Last edited:
Upvote 0
I tested your sample code and it is on the right track in randomize the selection when run multiple times,
Is there a way to add up the number to 11 instead of 1's , it will provide an add up lets just say 5+4+2?
Hi, I'm not sure I understand the adding up to 11 , 5+4+2.
Can you illustrate / provide the broad logic behind it?
 
Upvote 0
Hi , apologize if my explanation is not clear.
Please find the screenshot below.

Since the other column is added correctly to the green box for each row.
Would it be possible for it to randomly choose and add up the number base on the total number?

I hope this is clear

Furthermore , could the random chosen follow the existing number under "Count in AWB Raw Data"
For example , in BIS column for both blue and white box is not align on the existing AWB instead it is selecting for the person who did not do the BIS for the day

1719585135314.png
 
Upvote 0
OK, I think this is probably my best shot.

AutoTraining.xlsm
ABCDEFGHIJKLMNOPQR
1
2
331172421
4
5N0Name
61NM121
72NM281316111
83NM3153471
94NM4322
105NM5233131
116NM6644
127NM744
138NM8596
149NM9221
1510NM1031
1611NM11
1712NM1221
1813
1914
2015
2116
2217
2318
2419
2520
26
Sheet8 (2)


VBA Code:
Sub RandAudit()

Dim lr As Integer, os As Integer, i As Integer
Dim Auds As Integer, AudsCount As Integer, NameCount As Integer
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range
Dim Rng4 As Range, Rng5 As Range, Rng6 As Range
Dim Cel As Range


lr = Range("C" & Rows.Count).End(xlUp).Row
Set Rng1 = Range("K6:Q" & lr)
Set Rng2 = Range("S6:X" & lr)   'Columns S:X used as temporary 'helper' range
Set Rng3 = Range("S6:S" & lr)   'Column S used as temporary 'helper' range

Application.ScreenUpdating = False
Application.EnableEvents = False
'loop the randomisation up to 200 times to try and ensure all names get an audit
For i = 1 To 200
      Rng2.Formula = "=IF(D6>0,RAND(),"""")"
       Rng2.Value = Rng2.Value
    Rng1.Formula = "=IFERROR(IF(RANK(S6,S$6:S$" & lr & ",0)<=D$3,1,""""),"""")"
    Rng1.Value = Rng1.Value
    Rng2.ClearContents  'Clear the 'he;per' range
    
   'Use S as 'helper' column to check if all names have atleast 1 audit
   Rng3.Formula = "=OR(SUM(K6:Q6)>0,SUM(D6:J6)=0)"
    'If yes thenquit the randomise loop
    If Application.WorksheetFunction.CountIf(Rng3, True) = lr - 5 Then Exit For

Next

Rng3.ClearContents 'Clear the 'helper'

'Check column by column for need to allocate more than one audit
For os = 0 To 6  'column offset

    Set Rng4 = Range("K6:K" & lr).Offset(0, os)
    Set Rng5 = Range("D6:J" & lr).Offset(0, os)
    Set Rng6 = Range("D3").Offset(0, os)
        
    'only need to process if required audits exceed column's entries
    If WorksheetFunction.CountA(Rng4) < Rng6 Then
    On Error Resume Next   ' ignore potential runtime error if column has no entries
        NameCount = WorksheetFunction.CountA(Rng4)
         
        AudsBal = Rng6
        Auds = Int(AudsBal / NameCount)
        'check row by row and apportion required audits
            For Each Cel In Rng4.Cells
                If Cel > 0 Then
                     orig = Cel.Offset(0, -7)
                    Cel.Value = WorksheetFunction.Min(Auds, orig)
                    NameCount = NameCount - 1
                    AudsBal = AudsBal - Cel.Value
                    Auds = Int(AudsBal / NameCount)
                End If
                If NameCount = 0 Then Exit For
            Next Cel
        On Error GoTo 0  'reset error default
    End If
 
Next os     'next os  ie next column


Application.ScreenUpdating = True
Application.EnableEvents = True


End Sub

Hope that helps?
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,088
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