Using VB to export random rows based on date and name

djdbg1

New Member
Joined
Aug 23, 2017
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi All

I have been working on a project that monitors tasks being completed by users. An ask on this is that a people leader can use a button in excel, to generate a new sheet that provides 5 random rows for work competed on any given day, (Users will enter date as confirmation of task completion in column 17 and then their name in column 18.)

I have (with the help of the internet) got as far as the following code.

Code:
Sub dualfilter()

' dualfilter Macro
Dim strInput As String
Sheets("Customer Accounts").Select
strInput = InputBox("Enter date which you require a random sample for. Date to be entered in DD/MM/YYYY format. Where you see prompt to confirm deletion of tab, please hit OK")
Selection.AutoFilter
Sheets("Customer Accounts").Range("A:R").AutoFilter Field:=17, Criteria1:= _
strInput
Cells.Select
    Selection.Copy
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Select
    ActiveSheet.Name = "TempExtract"
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Select
    ActiveSheet.Name = "Extractpercentage"
    Sheets("TempExtract").Select
    Range("A1").Select
    ActiveSheet.Paste
  Randomize 'Initialize Random number seed
Dim MyRows() As Integer    ' Declare dynamic array.
Dim numRows, nxtRow, nxtRnd, chkRnd, copyRow As Integer
'Determine Number of Rows in Customer_Accounts Column A
  numRows = Sheets("TempExtract").Range("A" & Rows.Count).End(xlUp).Row
'Allocate 5 elements in Array
    ReDim MyRows(5)
'Create 5 Random numbers and fill array
     For nxtRow = 1 To 5
getNew:
'Generate Random number
      nxtRnd = Int((numRows) * Rnd + 1)
'Check for Header Row number (1)
      If nxtRnd = 1 Then GoTo getNew
'Loop through array, checking for Duplicates
       For chkRnd = 1 To nxtRow
'Get new number if Duplicate is found
        If MyRows(chkRnd) = nxtRnd Then GoTo getNew
       Next
'Add element if Random number is unique
      MyRows(nxtRow) = nxtRnd
     Next
'Loop through Array, copying rows to Sheet5
  For copyRow = 1 To 5
   Sheets("TempExtract").Rows(MyRows(copyRow)).EntireRow.Copy _
    Destination:=Sheets("Extractpercentage").Range("A2")(copyRow, 1)
    Next
  'Copy header row to newly created extract
    Sheets("Customer Accounts").Select
    Rows("1:1").Select
    Selection.Copy
    Sheets("Extractpercentage").Select
    Range("1:1").Select
    ActiveSheet.Paste
    'Delete Temp Extract Sheet as no longer required
    Sheets("TempExtract").Select
    ActiveWindow.SelectedSheets.Delete
    'Rename Exctract percentage with date and time it was created.
    Sheets("Extractpercentage").Select
    Sheets("Extractpercentage").Name = _
    WorksheetFunction.Text(Now(), "m-d-yyyy h_mm_ss am/pm")
    'Autofit columns
    Cells.Select
    Selection.ColumnWidth = 60
    Cells.EntireRow.AutoFit
    Cells.EntireColumn.AutoFit
End Sub

The above codes simply provides 5 random rows regardless of staff name in column 18. I want to generate data based on name and date. Additionally i won't necessarily want the same volume for each staff member. The desired sample will increase/decrease dependent on quality of work. Therefore, i am thinking adjacent to the button on sheet 1 that will start the macro, i can have a table that a people leader will complete with staff name and number of random rows they want extracted.

Code:
[TABLE="width: 345"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD][/TD]
[TD]   A[/TD]
[TD]     B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Staff name [/TD]
[TD]desired vol outputs[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Staff1[/TD]
[TD]     4[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Staff2[/TD]
[TD]     4[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Staff3[/TD]
[TD]     6[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Staff4[/TD]
[TD]     6[/TD]
[/TR]
</tbody>[/TABLE]

Finally, the staff and volume of staff won't be the same everyday, so one day when sample is produced there may only be 1 Staff and on other days there could be 5 staff.

I appreciate this is a chunky big request for support, any feedback and advise will be warmly received.

Thank you in advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,214,667
Messages
6,120,821
Members
448,990
Latest member
rohitsomani

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