Randomize List Based on Value

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
Hi, I'm trying to create a financial model simulating different scenarios or conditions. I have a list of 50 accounts. I want to create an input field where I set the value anywhere from 0 to 50. So say I choose 30, then I want a 1 to appear next to 30 of the 50 accounts (randomly). I will use this to create a formula to pull different sales values based on that. I'd image I need VB for this. Does anyone have any ideas?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi Rick, thank you for responding! The accounts are listed in B7:B56. However, there is a lot of data in between and I'm trying to get this random number generated in column 'AB'.
 
Upvote 0
Give this macro a try...
Code:
Sub ThirtyRandomAccounts()
  Dim LastRow As Long, Cnt As Long, RandomIndex As Long
  Dim Tmp As Variant, Arr As Variant
  LastRow = Range("B7").End(xlDown).Row
  Arr = Evaluate("ROW(B7:B" & LastRow & ")")
  For Cnt = UBound(Arr) To LBound(Arr) Step -1
    RandomIndex = Int((Cnt - LBound(Arr) + 1) * Rnd + LBound(Arr))
    Tmp = Arr(RandomIndex, 1)
    Arr(RandomIndex, 1) = Arr(Cnt, 1)
    Arr(Cnt, 1) = Tmp
  Next
  For Cnt = 1 To 30
    Cells(Arr(Cnt, 1), "AB").Value = 1
  Next
End Sub
 
Upvote 0
Hi Rick, thank you for responding. This set every cell within my range to a value of 1 instead of creating random numbers.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
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