vba生成四个随机时间间隔2分钟
以下是一个VBA代码示例,可以生成四个随机时间,间隔2分钟:
Sub GenerateRandomTimes()
Dim startTime As Date
Dim endTime As Date
Dim timeDiff As Integer
Dim randTime As Date
' set start and end time
startTime = "8:00 AM"
endTime = "10:00 PM"
' calculate time difference
timeDiff = DateDiff("n", startTime, endTime)
' loop to generate four random times
For i = 1 To 4
' generate random number
randNum = Int(Rnd() * timeDiff)
' add random number of minutes to start time
randTime = DateAdd("n", randNum, startTime)
' output random time
Debug.Print randTime
' add 2 minutes for next iteration
startTime = DateAdd("n", 2, randTime)
Next i
End Sub
该代码使用了VBA内置函数Rnd()来生成随机数,然后使用DateAdd()函数将随机数加到开始时间上,生成随机时间。在每次循环中,将开始时间增加2分钟,以保证四个随机时间之间的间隔为2分钟
原文地址: https://www.cveoy.top/t/topic/eCAn 著作权归作者所有。请勿转载和采集!