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
 
This tweak may apportion the multiple Audits more proportionally?

Book2
ABCDEFGHIJKLMNOPQ
1
2
331172421
4
5N0Name
61NM121
72NM281316111
83NM31534721
94NM4321
105NM5265319
116NM661
127NM741
138NM8596
149NM9221
1510NM1031
1611NM11
1712NM1221
1813
1914
2015
2116
2217
2318
2419
2520
26
Sheet1


VBA Code:
Sub RandAudit2()

Dim lr As Integer, os As Integer, i As Integer
Dim tot As Integer, orig 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
        'check row by row and apportion required audits
        
        tot = WorksheetFunction.Sum(Rng4.Offset(0, -7))
            For Each Cel In Rng4.Cells
                If Cel > 0 Then
                     orig = Cel.Offset(0, -7)
                     Auds = Int(orig * AudsBal / tot) + 1
                     If NameCount = 1 Then Auds = AudsBal
                    Cel.Value = WorksheetFunction.Min(Auds, orig)
                    NameCount = NameCount - 1
                    AudsBal = AudsBal - Cel.Value
                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
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This tweak may apportion the multiple Audits more proportionally?

Book2
ABCDEFGHIJKLMNOPQ
1
2
331172421
4
5N0Name
61NM121
72NM281316111
83NM31534721
94NM4321
105NM5265319
116NM661
127NM741
138NM8596
149NM9221
1510NM1031
1611NM11
1712NM1221
1813
1914
2015
2116
2217
2318
2419
2520
26
Sheet1


VBA Code:
Sub RandAudit2()

Dim lr As Integer, os As Integer, i As Integer
Dim tot As Integer, orig 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
        'check row by row and apportion required audits
       
        tot = WorksheetFunction.Sum(Rng4.Offset(0, -7))
            For Each Cel In Rng4.Cells
                If Cel > 0 Then
                     orig = Cel.Offset(0, -7)
                     Auds = Int(orig * AudsBal / tot) + 1
                     If NameCount = 1 Then Auds = AudsBal
                    Cel.Value = WorksheetFunction.Min(Auds, orig)
                    NameCount = NameCount - 1
                    AudsBal = AudsBal - Cel.Value
                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
Hi , i tested the code and it segregate the audit more appropriately.

However , multiple test result will maintain its calculation to 6+4+7 instead of randomizing the calculation . It this excel way of calculating the most efficient number?

Furthermore , would it be possible for the audit to capture on who is doing the allocated task for the day?
I try to tweak whether it can crosscheck if the number exist in this blue box it will put the audit number on that user
Currently , it is providing result on user who is not doing for the day

1719630098216.png
 
Upvote 0
Please find the mini sheet from the excel table that i am testing on.
Maybe the data from blue not showing proper whole number?

VBA Test_Random Audit.xlsm
BCDEFGHIJKLMNOPQ
3Total Volume shipment need to be Audit daily31172421* Enter the audit number in this Column K - Column Q
4Count of AWB In Raw DataThe Number Of Shipment Need To Be Audit
5NoACSBISDODHRSCHWRU/BYVEACSBISDODHRSCHWRU/BYVE
61Angela221220
72Jafni181316110
83Jia1554970111
94Jing1116101
105Vijaya23173101
116Mara6461
127Nayan604
138Rian59711
Audit Volume Matrix
 
Upvote 0

Forum statistics

Threads
1,222,095
Messages
6,163,901
Members
451,865
Latest member
dunworthc

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