Random Generation from list

GrayJ83

New Member
Joined
Sep 29, 2016
Messages
21
Greetings,

I am very green at VBA, so I don't even have a sample code to give.

I want to have two columns of words say [A1:A10] and [B1:B10], randomly select a word in the first column (the A column) and print it in D1, then randomly select a word from the second column the B column) and print it in E1.

And be able to repeat the macro, with out having to delete the printed values in D1 and E1.

Thank you for any help that is given.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Place this code in your data sheet module:-
Select "C1" or "E1" for related results from "A1:A10" Or "B1:B10"

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("D1:E1")) Is Nothing Then
    Select Case Target
        Case Range("D1"): Target = Cells(Application.RandBetween(1, 10), 1)
        Case Range("E1"): Target = Cells(Application.RandBetween(1, 10), 2)
    End Select
End If
End Sub
 
Upvote 0
I apologies, I am assuming the date sheet module is the window where you enter the macro? And it does not seem to save when I click the Run button it shows the Macro window were you create the new macro, with it blank.
 
Last edited by a moderator:
Upvote 0
To load the code:-
Right click your data sheet tab, From the dropdown menu, select, "View Code".
VB Code Window appears. Paste the code into this window, Close Vbwindow.
Now when you select either "D1" or "E1" the code should run and a new value from either "A1:A10" or "B1":B10" will appear in the selected cell.
 
Upvote 0
Hi Mick, a couple of comments about your suggested code.
- If you select, say, D1 and get a random value and you decide you want a new random value in that same cell, you have to actually select a different cell and then come back to D1. That would annoy me.
- If the user happens to select D1:E1 (or any multi-cell range that overlaps D1:E1, the code errors.

GrayJ83
Because of the above issues, I would suggest using the Double-Click event instead. To test it you would remove Mick's code and try this, implemented in the same way and then test by double-clicking D1 and/or E1

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  If Not Intersect(Target, Range("D1:E1")) Is Nothing Then
    Randomize
    Cancel = True
    Target.Value = Cells(1 + Int(Rnd() * 10), Target.Column - 3).Value
  End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top