I want to use a function to select a random number between 1 and 31, and based on the selection, assign a "box" value. Results 1-16 are Box 1, 17-24 are Box 2, etc.
Within the Sub, I want to detect whether the selected box exists within the data. Here's the important part: If the selected box doesn't exist within the data, select a different box.
To select a different box, I want to refer to the function rather than having to loop using "goto", because looping gets very messy very quickly.
Similar questions seem to be use cases different from mine. The values I'm trying to generate are not determined by variables defined in the Sub, as is a common use of functions.
This is on Windows 10, Excel 365. I've used looping (GoTo... #) because I'm not experienced enough with functions.
Sub cmdClickityClack
SelectABox
MsgBox (SelectABox(Box)) 'I know, this is completely wrong
End Sub
Function SelectABox(Box As Long)
Dim BoxFind As Long
BoxFind = RndBetween(1, 31)
Select Case BoxFind
Case 0 To 16
Box = "1"
Case 17 To 24
Box = "2"
Case 25 To 28
Box = "3"
Case 29 To 30
Box = "4"
Case 31
Box = "5"
End Select
End Function
Ideally, I'd just use the variable Box, which was defined by the function, within the sub. The sub would know the value of Box after the function is called.