Select Random Data Automatically

How to select random data automatically using index function in VBA. Watch the video below:

Select Random Data Automatically

Often we may need to select a few customer names from a large list of regular customers so that we can offer them a discount. The best way to do the selection is to use random automation to avoid any controversy. In a similar fashion a website may offer a badge to a few selected regular contributors to motivate everyone to participate.

Here’s the complete VBA code to select random data automatically using the Index worksheet function:

Sub SelectRandomData()
Dim MyRange As Range
Dim lastrow As Long
Dim lMyRandomRow As Long
Dim lRows As Long
Dim i As Long
lastrow = Application.WorksheetFunction.CountA(Range(“A:A”))
Set MyRange = Sheet1.Range(“A2:A” & lastrow)
lRows = MyRange.Rows.Count
Range(“C1”) = “Selected Customers”
Range(“C1”).Font.Bold = True
For i = 2 To 11
lMyRandomRow = Int(Rnd() * lRows) + 1 Cells(i, 3) = Application.WorksheetFunction.Index(MyRange, lMyRandomRow, 1)
Next i
End Sub
VBA code to select random data
Select Random Data Automatically
Select Random Data Automatically

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.