mercredi 3 février 2021

VBA - Picking up a value from list without repeats

I'm writing a code which chooses "x" client ids ​​then creates a loop that randomly chooses "x" ids ​​from a list, after this the code do a xlookup to get revenue from this clients, and finally sum all the chosen clients revenues, so far so good, but I don´t know how to choose randomly without repeating the previous id of the loop. I don't know if I was clear enough. Follow code below

Private Sub pick()


resultado = 0

For X = 1 To Range("G7").Value
valor_procurado = WorksheetFunction.Choose(WorksheetFunction.RandBetween(1, 10), _
Sheets("lista").Range("a1"), _
Sheets("lista").Range("a2"), _
Sheets("lista").Range("a3"), _
Sheets("lista").Range("a4"), _
Sheets("lista").Range("a5"), _
Sheets("lista").Range("a6"), _
Sheets("lista").Range("a7"), _
Sheets("lista").Range("a8"), _
Sheets("lista").Range("a9"), _
Sheets("lista").Range("a10"))

matriz_procurada = Sheets("lista").Range("a1:a10")

matriz_retorno = Sheets("lista").Range("b1:b10")

valores = WorksheetFunction.XLookup(valor_procurado, matriz_procurada, matriz_retorno, "-", 0)

resultado = resultado + valores

Next

Range("G8") = resultado

End Sub



Aucun commentaire:

Enregistrer un commentaire