Solved

Excel Random Generator

Posted on 2013-05-12
4
314 Views
Last Modified: 2013-05-12
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.

Thank you in advance,

B.
Random-Generator.xlsm
0
Comment
Question by:Bright01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Expert Comment

by:als315
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

by:Bright01
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 81

Accepted Solution

by:
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

Open in new window

Random-GeneratorQ28125659.xlsm
0
 

Author Closing Comment

by:Bright01
ID: 39160524
Byundt,

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

All the best,

B.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question