Solved

# Excel Random Generator

Posted on 2013-05-12
306 Views
EE Pros,

I am trying to build a small, simple macro that randomly generates responses into a very specific set of cells when you push a button.

I have two columns separated by 3 categories, with 5 cells representing answers to each category; populated from two different Range lists (ListCurrent, ListFuture).

There are 3 categories that separate 5 rows which are Questions where the random results are "the Answers (Answer1 Answer2).

I've attached a sample for you to see what I'm trying to accomplish.  Please add some comments so I know how to scale it.

B.
Random-Generator.xlsm
0
Question by:Bright01
• 2

LVL 39

Expert Comment

ID: 39159839
You can enter this formula to your cells (Column C):
=INDIRECT("J" & RANDBETWEEN(49,54),TRUE)
and
=INDIRECT("K" & RANDBETWEEN(49,54),TRUE)
and values will be changed every time when your worksheet will be realculated (you can also press F9)
0

Author Comment

ID: 39159958
Als325, unfortunately a formula won't work (need a macro).  You see, the random generator is to demonstrate what the model looks like after it is populated.  I have to keep the cells clear because new data is going to be entered directly into the cells after the model is cleared (with a .clear command.

Make sense?

B.
0

LVL 80

Accepted Solution

byundt earned 500 total points
ID: 39160363
Here's a macro that puts random responses in a specified range of cells. As written, the code runs on the active worksheet. The statements with comments may require tweaks to suit your actual workbook.
``````Sub RandomResponse()
Dim v As Variant, vNames As Variant
Dim i As Long, j As Long, n As Long, nResponses As Long
Dim rg As Range, rgResponses As Range, rw As Range
Application.ScreenUpdating = False
Set rg = ActiveSheet.Range("C49:C66")           'First column of random responses go here. Additional responses in successive columns.
vNames = Array("ListCurrent", "ListFuture")     'Names of ranges containing possible responses
For Each v In vNames
j = j + 1
i = 0
Set rgResponses = ActiveWorkbook.Names(v).RefersToRange
nResponses = rgResponses.Cells.Count
For Each rw In rg.Rows
i = i + 1
If (i Mod 6) <> 1 Then      'Need some means to avoid putting values in row with Category name
rw.Cells(1, j).Value = rgResponses.Cells(Int(Rnd() * nResponses + 1))
End If
Next
Next
End Sub
``````
Random-GeneratorQ28125659.xlsm
0

Author Closing Comment

ID: 39160524
Byundt,

Perfect!  Just what I described.  And.....looks like it will scale for me.

All the best,

B.
0

## Featured Post

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.