Pick a row randomly for each user

sunilbsrv2k

Board Regular
Joined
May 25, 2018
Messages
73
Hi All,

I am having a set of data containing several rows and columns.

This data contains "User Name" in one of the columns; however another column contains the Date on which he accessed a document. One more column has this document ID.

Now, I need to randomly pick the entire row for each of the users but needs to be one row per day per user. It should cover for all the users of that day and paste it in a new sheet.

Request you to kindly help.

Thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi,

I guess I have complicated my question.

In short, I want my code to pick a row randomly based on the username and the date.

Please help.

Thanks,
 
Upvote 0
Hi All,
I am trying different method to pick a row randomly.

This time, I am filtering the data for each user and trying to pick a row in that. However, I am stuck at the point of copying the row, as the Rnd function is giving me decimal results. Could you please help.

VBA Code:
Sub pickRandomly()
Dim wb, wb1 As Workbook
Dim ws, ws1 As Worksheet
Dim rng, rng1 As Range
Dim i, n As Integer

Set wb = ThisWorkbook

Set ws = wb.Worksheets(1)

Set rng = ws.Range("B10:B17")
Rndm = Rnd(5)
MsgBox Rndm
For Each cell In rng
    'MsgBox cell
    n = 0
    Worksheets("Sheet1").Range("A22").AutoFilter Field:=2, Criteria1:=cell.Value
    i = Application.WorksheetFunction.Subtotal(3, Range("A23:A1000"))
    'MsgBox i
    For n = 1 To i
        If n = Rndm Then
            MsgBox Cells(n + 20, 1).Value
        End If
    Next
    MsgBox n
Next

End Sub

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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