Sta*_*cie 2 random excel vba excel-vba
我试图能够从一个数组返回一个字符串,该数组将被放入userform中的标签.我已经看到很多东西,但它们似乎都没有用.我有一个名为Compliments的数组,我想拉出该数组的一个字符串并将其传递给我的label6但是因为这是一个只在打开工作表时显示的用户表单并且暂时打开我希望能够让它随机.每当有人打开表格时,他们都会得到不同的赞美,以帮助他们开始新的一天.我的所有代码如下:
Private Sub UserForm_Activate()
TextBox1.Value = Date
TextBox2.Value = Time
TextBox3.Value = MainMenu.TextBox1.Value
Label6.Caption = "RANDOM ARRAY GOES HERE"
Application.Wait (Now + TimeValue("00:00:05"))
Welcome.Hide
End Sub
Private Sub UserForm_Initialize()
Me.StartUpPosition = 0
Me.Top = (Application.Height / 2) - Me.Height / 2
Me.Left = (Application.Width / 2) - Me.Width / 2
Compliments = Array("Good Morning, You are Beautiful Today", _
"I think you're pretty awesome", "That outfit looks great on you", _
"You're a great engineer", "You rock Dude", "Nobody can get you down", _
"Your makeup is spot on")
End Sub
Run Code Online (Sandbox Code Playgroud)
您可以使用VBA中的Rnd()函数生成随机数.通过一些数学,你可以强制它在两个整数之间.在这种情况下,它将介于0和Compliments数组的上限之间.像下面这样的东西应该工作:
Function Compliments()
'Function to return the array
Compliments = Array("Good Morning, You are Beautiful Today", _
"I think you're pretty awesome", "That outfit looks great on you", _
"You're a great engineer", "You rock Dude", "Nobody can get you down", _
"Your makeup is spot on")
End Function
Private Sub UserForm_Activate()
TextBox1.Value = Date
TextBox2.Value = Time
TextBox3.Value = MainMenu.TextBox1.Value
randArrIndex = Int ((Ubound(Compliments) + 1) * Rnd )
Label6.Caption = Compliments(randArrIndex)
Application.Wait (Now + TimeValue("00:00:05"))
Welcome.Hide
End Sub
Private Sub UserForm_Initialize()
Me.StartUpPosition = 0
Me.Top = (Application.Height / 2) - Me.Height / 2
Me.Left = (Application.Width / 2) - Me.Width / 2
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
673 次 |
| 最近记录: |