Entry into a randomly selected cell

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
I would like to run a macro that (1) randomly selects one cell in range A1:M20 to enter "Start here". Each time the macro is run, "Start here" would likely appear in a different cell, although it is possible the macro could select the same one. (2) Once "Start here" appears in a cell, a randomly generated number between 1 and 10 should appear two cells to the right and two rows down.

Can this be done? Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about
VBA Code:
Sub ChristineJ()
   Dim x As Long
   
   Randomize
   With Range("A1:M20")
      x = Int(.Count * Rnd() + 1)
      .Item(x).Value = "Start here"
      .Item(x).Offset(2, 2).Value = Application.RandBetween(1, 10)
   End With
End Sub
 
Upvote 0
Works perfectly and is just what I need. Thank you so much for your quick reply. CJ
 
Upvote 0
You're welcome & thanks fr the feedback.
 
Upvote 0
Could I ask one more question related to this item in the code.

Is it possible to use this result of .Item(x).Offset(2, 2).Value = Application.RandBetween(1, 10) as a variable to determine another amount?

Example
.Item(x).Offset(3, 3).Value = .Item(x).Offset(2, 2).Value +25
So if .Item(x).Offset(2, 2).Value = 5, .Item(x).Offset(3, 3).Value would = 30

Thanks! CJ
 
Upvote 0
How about
VBA Code:
Sub ChristineJ()
   Dim x As Long, y As Long
   
   Randomize
   y = Application.RandBetween(1, 10)
   With Range("A1:M20")
      x = Int(.Count * Rnd() + 1)
      .Item(x).Value = "Start here"
      .Item(x).Offset(2, 2).Value = y
      .Item(x).Offset(3, 3) = y + 25
   End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,863
Messages
6,121,978
Members
449,058
Latest member
oculus

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