3 Ways to Generate Large Amounts of Data

How to generate large amounts of data for analysis using 3 ways in VBA. Watch the video below:

3 methods to generate large amounts of data

Often we need data for practice especially numerical data like sales, Wouldn’t it be helpful to generate such data automatically and quickly? We give below 3 macros to generate such data quickly and easily. Using arrays we can generate almost one million rows of data within 15 seconds. The other methods are relatively slow and require about 45 minutes each.

Sub FillRangeUsingArray()
Dim rngCurrent As Range
Dim lngRows As Long
Dim lngCols As Long

Dim lngR As Long
Dim lngC As Long
Dim varValues() As Variant

Set rngCurrent = Range("B2:E999500")
Dim startTime As Date, endTime As Date
startTime = Timer
lngRows = rngCurrent.Rows.Count
lngCols = rngCurrent.Columns.Count
ReDim varValues(0 To lngRows - 1, 0 To lngCols - 1) As Variant
For lngR = 0 To lngRows - 1
For lngC = 0 To lngCols - 1
varValues(lngR, lngC) = Int((120000 - 60000 + 1) * Rnd + 30000)
Next
Next
rngCurrent.Value = varValues()
endTime = Timer
MsgBox Format(endTime - startTime, "0.0")
End Sub
Generate Random Data Using arrays
Sub FillRange()
Dim Col As Long
Dim Row As Long
Dim startTime As Date, endTime As Date
startTime = Timer
For Col = 2 To 5
For Row = 2 To 999500
‘To create a random integer number between two values, we can use the following formula
‘Int ((upperbound – lowerbound + 1) * Rnd + lowerbound)
Cells(Row, Col) = Int((120000 – 30000 + 1) * Rnd + 30000)
Next Row
Next Col
endTime = Timer
MsgBox Format(endTime – startTime, “0.0”)
End Sub
Generate Random Data using For Next Loop
Sub FillRange2()
‘Const MyRange = “$B$2:$E$999500”
Dim cell As Range
Dim startTime As Date, endTime As Date
startTime = Timer
For Each cell In Range(“B2:E999500”)
cell = Int((120000 - 30000 + 1) * Rnd + 30000)
Next cell
endTime = Timer
MsgBox Format(endTime – startTime, “0.0”)
End Sub
Using For Each Next Loop to Generate Random Numbers
3 Ways to Generate Large Amounts of Data

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.