How to select random rows from an excel file and copy over to another excel file


I am trying to select random rows from an excel document (e.g. 100 rows i need 30 random rows) and copy it over to another excel file. Currently i am trying AsEmunerable but it only gives me the first 30 rows.

Is there anything wrong with this or is there any alternative solutions.

As an alternate you can create a list of Index numbers like this

With the distinct we do run till we got 30 unique index values.

The rows can be selected with: RandomList.Select(Function (x) DataTableVar.Rows(x)).CopytoDataTable



Do you have the workflow?

Thanks alot for the help!

Find starter help for generating the random Index numbers here:
Needhelp.xaml (6.0 KB)


Give this a try: (29.7 KB)

1 Like

This is really helpful
Is there away to select the number of records that I want to randomly select?

Lets say I want 30 from the whole population

Hi @HKF,
In the code attached, you’ll find an argument called “_countRowsToPick_in”. You’ll have to set it to how many rows you’d like to randomly select.

Thanks a lot your file is really helpful

My report has 300 records
I put the number to be randomly selected is 5
Most of times it get me 5
But sometimes I get 4
Sometimes I get 3

Do you know how to fix this

That’s true.
I made some adjustments to the code, give it a try: (11.4 KB)


Thanks a lot :+1:t3::+1:t3:

This was extremely helpful for me in a similar process. Thank you.