VBA or Formula to populate range of cells with set frequency of pre-defined values.

bartmaster

New Member
Joined
Jan 30, 2019
Messages
21
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

Please see below table:
Book2
ABCDEFG
1ValuesValueOccurrenceNew count of values:65
2BBBAAA10%
3AAAAAB5%Values:
4AABABA15%
5ABAABB15%
6AAABAA15%
7ABBBAB15%
8BABBBA15%
9BBABBB10%
10BBB
11ABA
12BAB
13BAA
14BBA
15ABB
16BAA
17BBA
18ABA
19ABB
20BAB
21BAA
Sheet1
Cell Formulas
RangeFormula
D2:D9D2=COUNTIF($A$2:$A$21,C2)/20


Column C contains a list of distinct values from column A with their percentage distribution in column D.
Easy enough!

The question is how can we revert the process to get a list of pre-defined valuesfrom column C into column G (starting from row 4).
I would want to have them in random order but keeping the set distribution percentage?
Additionally a variable in the cell G1 would tell how many rows we would like to fill, i.e. currently column A has 20 values but in column F we would like to get a list of 65 values.

Any ideas? :)
Many thanks
 
Would it be too much to ask you to write a brief step by step explanation?
See below.

VBA Code:
Sub New_List()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, Tsum As Long, NewCount As Long
  
  'Required to ensure random numbers generated later are truly random
  Randomize
  'Read existing values & occurrence %s into an array
  a = Range("C2", Range("D" & Rows.Count).End(xlUp)).Value
  'Record the number of new values required
  NewCount = Range("G1").Value
  'Starting at the 2nd value calculate no. of each value required to get the new total ..
  For i = 2 To UBound(a)
    '.. by multiplying the % from col D and the new total no.
    a(i, 2) = Round(a(i, 2) * NewCount, 0)
    'Keep a running total
    Tsum = Tsum + a(i, 2)
  Next i
  'Now calculate the no. required for the 1st value to account for rounding in the other calculations
  a(1, 2) = NewCount - Tsum
  'Create an array to hold the new values
  ReDim b(1 To NewCount, 1 To 2)
  'Work through the 'a' array ..
  For i = 1 To UBound(a)
    '.. to make the required no. of each value, not random at this point
    For j = 1 To a(i, 2)
      'On each row of the 'b' array
      k = k + 1
      'Write the relevant value ..
      b(k, 1) = a(i, 1)
      '.. & beside it a random number
      b(k, 2) = Rnd
    Next j
  Next i
  Application.ScreenUpdating = False
  'In cols G:H & required no. of rows
  With Range("G4:H4").Resize(NewCount)
    'Enter the values & random no.s from the 'b' array
    .Value = b
    'Sort these values by the random no.s in col H
    .Sort Key1:=.Columns(2), Header:=xlNo
    'Now clear out the random no.s
    .Columns(2).ClearContents
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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