在VBA 编程中怎样选出6个幸运号

时间:2024-10-14 15:55:00

1、打开excle软件,进入VBA的编程界面,新建一个子过程。

在VBA 编程中怎样选出6个幸运号

2、定义两个常量。一个最大值,一个最小值,等一下我们要选出在1-60之间的6个幸运号码。Const minNum = 1 Const maxNum = 60

在VBA 编程中怎样选出6个幸运号

3、然后定义变量,t,i,用来循环的变量,lucky是用来存储幸运号码的数组。Dim t As Integer Dim i As Integer Dim lucky(6) As String

在VBA 编程中怎样选出6个幸运号

4、然后进行第一层循环。

在VBA 编程中怎样选出6个幸运号

5、进行第二层循环,检查是否有重复的数字。

在VBA 编程中怎样选出6个幸运号

6、将结果打印出来。完整代码如下:Sub find_lucky() Const minNum = 1 Const maxNu罪焐芡拂m = 60 Dim t As Integer Dim i As Integer Dim lucky(6) As String For t = 1 To 6 Randomize lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum For i = 1 To (t - 1) If lucky(t) = lucky(i) Then lucky(t) = Int((maxNum - minNum + 1) * Rnd) + minNum i = 0 End If Next i MsgBox "第" & t & "个luckynumber is" & lucky(t) Next t End Sub

在VBA 编程中怎样选出6个幸运号

7、最后运行程序即可。

在VBA 编程中怎样选出6个幸运号
在VBA 编程中怎样选出6个幸运号
© 手抄报圈