Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick random value from string array

Tags:

excel

vba

How do I make the following code input either "Jack" or "John" randomly in cell A1? Currently, the result is always "2":

Sub RandomNames ()

Dim UserNames(1 To 2) As String

UserNames(1) = "Jack"
UserNames(2) = "John"

Range("A1").Value = Application.WorksheetFunction.RandBetween(LBound(UserNames), UBound(UserNames))

End Sub
like image 464
barciewicz Avatar asked Jul 04 '26 08:07

barciewicz


1 Answers

Try using randbetween on the array.

Range("A1").Value = UserNames(Application.RandBetween(LBound(UserNames), UBound(UserNames)))

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!