Excel:vba实现生成随机数

复制代码
Sub 生成随机数字()
    Dim randomNumber As Integer
    Dim minValue As Integer
    Dim maxValue As Integer
    
    ' 设置随机数的范围(假入班级里面有43个学生,学号是从1→43)
    minValue = 1
    maxValue = 43
    
    ' 生成随机数(在1到43之间生成随机数)
    randomNumber = Application.WorksheetFunction.RandBetween(minValue, maxValue)
    
    ' 显示结果
    MsgBox "生成的随机数字是: " & randomNumber
End Sub

随机点名:(可能会出现重复的)

复制代码
Sub 随机点名()
    Dim randomIndex As Integer
    Dim selectedName As String
    Dim 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
相关推荐
Zzq_Fighting1 小时前
Excel批量匹配数据
excel
E_ICEBLUE2 小时前
Python 实现 Excel 转 Markdown,支持工作表、单元格区域和批量处理
python·excel·markdown·格式转换
拆房老料2 小时前
ONLYOFFICE 集群部署实战:9.3 多实例方案与 9.4 架构变化
开源·word·excel·开源软件·ppt
ms365copilot4 小时前
Excel Copilot挖掘数据洞察,不用精通函数也能做数据分析 (1)
数据分析·excel·copilot
babe小鑫4 小时前
精算学专业秋招:Excel、SQL、R和Python应该先练什么
sql·r语言·excel
医疗信息化王工13 小时前
DataForge:基于 Python 的数据库批量导出 Excel 工具——从架构到部署的全流程实战
数据库·python·excel
1314lay_100720 小时前
通过Sql Server创建EXCEL文件:master..xp_cmdshell
数据库·excel
2601_962300811 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
ms365copilot1 天前
Excel Copilot一键美化图表,无需手动调格式
excel·copilot·outlook
2501_933670791 天前
内部审计校招的数据化趋势:SQL、Excel、Power BI和证书选择
数据库·sql·excel