用VBA编辑ActionX命令控件,在单击时随机打开其中一个网站
- 插入命令控件
右键命令控件-属性表:更改基本属性; 右键命令控件-查看代码
- 随机数
temp = Int(Rnd() * 4) + 1
Rnd()为0,1之间的随机函数;Int()为取整;由此定义temp为1,4之间的随机数
- 打开网站
Shell "explorer.exe https://www.baidu.com" & Text1
完整代码
'针对命令控件单击动作编辑
Private Sub Button_Click()
'重置随机器
Randomize
'随机一个1,4之间的整数
temp = Int(Rnd() * 4) + 1
'执行打开网站的操作
If temp = 1 Then Shell "explorer.exe https://www.baidu.com" & Text1
If temp = 2 Then Shell "explorer.exe https://www.baidu.com" & Text1
If temp = 3 Then Shell "explorer.exe https://www.baidu.com" & Text1
If temp = 4 Then Shell "explorer.exe https://www.baidu.com" & Text1
End Sub
感谢@C