VBA example - Generating Random Numbers

If you are conducting a randomized clinical trial, you need to assign patients randomly into the "treatment group" and "control group". It can be achieved by the following code.

'generate random numbers, and put
'patients in different groups based
'on the random number

Sub RandomNumber()
    Dim row As Integer
    Dim r As Single
    For row = 2 To 17
        r = Rnd()
        Cells(row, 2) = r
        Dim group As String
        If r < 0.5 Then
            group = "Treatment group"
        Else
            group = "Control group"
        End If
        Cells(row, 3) = group
    Next
    MsgBox ("done")
End Sub

The result is below.

Comments

Popular posts from this blog

Excel - IF function

Excel - VLOOKUP function

Excel - Hierarchical chart (Treemap)