dimanche 26 février 2023

Weighted random name selector from an external workbook

I'm new to VBA but have some coding background. The below must be done through Excel.

Objective: List of names that need to be selected at random, but the names will have weighting to them so that some names are more likely to be selected. The table of data will always be an external file, and the data cannot be printed to the main worksheet.

Example of the data that will be read:

ID Name Weighting
1 Person 1 3
2 Person 2 1
3 Person 3 2
4 Person 4 5
5 Person 5 1

So Person 4 will be 5 times more likely to be picked than Person 5, for example.

I've been going around in circles so far. I thought adding the data to an array and then looping through that array to find a weighting,array1(3,i) that is greater than 1, then grabbing the name to the left array1(2,i) and adding it again to the end of the array would work.

However, I have since found out you cannot increase the first dimension of an array dynamically in VBA.

I am initially declaring my array with array1 = wb.Worksheet(1).ListObjects("Table1").DataBodyRange.Value, where wb is the open workbook captured through GetOpenFilename().

Any idea what I can try? I would print my code, however my PC just reset and I lost the last save state, but can attempt a rewrite if it would be useful?

I have followed various examples/tutorials online but I think this exact scenario doesn't seem to be covered, and with my lack of VBA knowledge I'm at a loose end.




Aucun commentaire:

Enregistrer un commentaire