Dear Community,
I am trying to create a macro to do what it is so called "random selection proportional to size".
<tbody>
[TD="align: center"] City [/TD]
[TD="align: center"] Population (.000) [/TD]
[TD="align: center"] Cummulative population [/TD]
[TD="align: center"] Selected [/TD]
[TD="align: center"]London[/TD]
[TD="align: center"] 2.000[/TD]
[TD="align: center"] 2.000[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Madrid[/TD]
[TD="align: center"] 1.500[/TD]
[TD="align: center"] 3.500[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Paris[/TD]
[TD="align: center"] 1.800[/TD]
[TD="align: center"] 5.300[/TD]
[TD="align: center"]1st[/TD]
[TD="align: center"]Prague[/TD]
[TD="align: center"] 900[/TD]
[TD="align: center"] 6.200[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Lisbon[/TD]
[TD="align: center"] 600[/TD]
[TD="align: center"] 6.800[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Zurich[/TD]
[TD="align: center"] 950[/TD]
[TD="align: center"] 7.750[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Bern[/TD]
[TD="align: center"] 350[/TD]
[TD="align: center"] 8.100[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Barcelona[/TD]
[TD="align: center"] 1.300[/TD]
[TD="align: center"] 9.400[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Berlin[/TD]
[TD="align: center"] 1.200[/TD]
[TD="align: center"] 10.600[/TD]
[TD="align: center"]2nd[/TD]
[TD="align: center"]Brujas[/TD]
[TD="align: center"] 430[/TD]
[TD="align: center"] 11.030[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Toledo[/TD]
[TD="align: center"] 150[/TD]
[TD="align: center"] 11.180[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Dublin[/TD]
[TD="align: center"] 620[/TD]
[TD="align: center"] 11.800[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Liverpool[/TD]
[TD="align: center"] 1.700[/TD]
[TD="align: center"] 13.500[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Amsterdam[/TD]
[TD="align: center"] 1.100[/TD]
[TD="align: center"] 14.600[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Oslo[/TD]
[TD="align: center"] 530[/TD]
[TD="align: center"] 15.130[/TD]
[TD="align: center"]3rd[/TD]
</tbody>
hope I have explained properly.
Thanks in advance!
Daniel
Edit:
I forgot to show the result
[TABLE="class: cms_table"]
<tbody>[TR]
[TD]Cities Selected[/TD]
[/TR]
[TR]
[TD]Paris[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[/TR]
[TR]
[TD]Oslo
[/TD]
[/TR]
</tbody>[/TABLE]
I am trying to create a macro to do what it is so called "random selection proportional to size".
- I have a table named "source" with names of cities in column B, population in each city in column C and the cumulative population for each row in column D.
- I have a cell (F5) where the user must enter the number of cities to select
- I have another table called "result" where the cities selected must be pasted
- Add the population in all cities (last cell in column D)
- Divide the total population by the number of cities to select (F5).
- Randomly select one city from the table (this will be the first city and the name must be pasted in the "results" table)
- Add the result of step 2 to the cumulative population corresponding to this city (the value in column D for the same row)
- Search the result of step 4 in column D until you find a row where the cumulative number that is higher than the result in step 4
- The city corresponding to this value (in the same row) is the second city and its name must be pasted in table "results" below the first city selected
- This must be repeated until we have the number of cities in cell F5
- If the result of step 4 is higher than the total population, then substract the total population (15130) and continue the process from the beginning
<tbody>
[TD="align: center"] City [/TD]
[TD="align: center"] Population (.000) [/TD]
[TD="align: center"] Cummulative population [/TD]
[TD="align: center"] Selected [/TD]
[TD="align: center"]London[/TD]
[TD="align: center"] 2.000[/TD]
[TD="align: center"] 2.000[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Madrid[/TD]
[TD="align: center"] 1.500[/TD]
[TD="align: center"] 3.500[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Paris[/TD]
[TD="align: center"] 1.800[/TD]
[TD="align: center"] 5.300[/TD]
[TD="align: center"]1st[/TD]
[TD="align: center"]Prague[/TD]
[TD="align: center"] 900[/TD]
[TD="align: center"] 6.200[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Lisbon[/TD]
[TD="align: center"] 600[/TD]
[TD="align: center"] 6.800[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Zurich[/TD]
[TD="align: center"] 950[/TD]
[TD="align: center"] 7.750[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Bern[/TD]
[TD="align: center"] 350[/TD]
[TD="align: center"] 8.100[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Barcelona[/TD]
[TD="align: center"] 1.300[/TD]
[TD="align: center"] 9.400[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Berlin[/TD]
[TD="align: center"] 1.200[/TD]
[TD="align: center"] 10.600[/TD]
[TD="align: center"]2nd[/TD]
[TD="align: center"]Brujas[/TD]
[TD="align: center"] 430[/TD]
[TD="align: center"] 11.030[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Toledo[/TD]
[TD="align: center"] 150[/TD]
[TD="align: center"] 11.180[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Dublin[/TD]
[TD="align: center"] 620[/TD]
[TD="align: center"] 11.800[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Liverpool[/TD]
[TD="align: center"] 1.700[/TD]
[TD="align: center"] 13.500[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Amsterdam[/TD]
[TD="align: center"] 1.100[/TD]
[TD="align: center"] 14.600[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Oslo[/TD]
[TD="align: center"] 530[/TD]
[TD="align: center"] 15.130[/TD]
[TD="align: center"]3rd[/TD]
</tbody>
hope I have explained properly.
Thanks in advance!
Daniel
Edit:
I forgot to show the result
[TABLE="class: cms_table"]
<tbody>[TR]
[TD]Cities Selected[/TD]
[/TR]
[TR]
[TD]Paris[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[/TR]
[TR]
[TD]Oslo
[/TD]
[/TR]
</tbody>[/TABLE]
Last edited by a moderator: