Sub 生成随机数字()Dim randomNumber As IntegerDim minValue As IntegerDim maxValue As Integer' 设置随机数的范围(假入班级里面有43个学生,学号是从1→43)minValue = 1maxValue = 43' 生成随机数(在1到43之间生成随机数)randomNumber = Application.WorksheetFunction.RandBetween(minValue, maxValue)' 显示结果MsgBox "生成的随机数字是: " & randomNumber
End Sub
随机点名:(可能会出现重复的)
Sub 随机点名()Dim randomIndex As IntegerDim selectedName As StringDim lastrow As Integer'获取最后一行lastrow = Cells(Rows.Count, 1).End(xlUp).Row' 生成随机索引,即名字前面的行号(在第一行到最后一行之间进行选择)randomIndex = Application.WorksheetFunction.RandBetween(1, lastrow)' 获取选中的名字selectedName = Cells(randomIndex, 1).value' 显示结果MsgBox "选中的名字是: " & selectedName
End Sub