我对编码非常陌生,只想感谢这个站点上对我有很大帮助的每个人。
我正在尝试构建一个随机时间生成器,该生成器将在用户指定的范围内生成随机时间。我当前正在使用WorksheetFunction.Randbetween命令,该命令可以有效地生成随机时间,但是它不在指定范围内。此代码是执行类似功能的较大用户窗体的一部分。其余代码可以正常工作。
我的问题:我需要在代码行中添加哪些内容才能有效地生成指定范围内的时间?我还需要确保它可以使用24小时“ HH:MM:SS”或“ HH:MM”格式。
我尝试使用Round和WorksheetFunction.Randbetween
Private Sub Generate_Data_Button_Click()
Dim emptyRow As Long
Randomize
'Make Data Active
Sheet1.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A"))+1
'Transfer information to template
Cells(emptyRow, 4).Value = WorksheetFunction.Randbetween (start_time_textbox.value, end_time_textbox.value) + Rnd()
'Format cell on template
Cells(emptyRow, 4).NumberFormat = "hh:mm:ss"
End Sub
答案 0 :(得分:2)
随机化/随机化方法
在Randomize
之后,Rnd
每次被调用时都会生成一个介于零和一之间的伪随机十进制数。如果您的时间是全时值(例如23:59:45),则使用TimeValue字符串到双精度转换来转换输入框返回的字符串,并使用一些数学运算将Rnd值强制放入开始和停止位置时间。
Private Sub Generate_Data_Button_Click()
Dim emptyRow As Long, tstart As Double, tend As Double
'convert string times from input to true time as double
tstart = TimeValue(start_time_textbox.Value)
tend = TimeValue(end_time_textbox.Value)
Randomize
'Make Data Active
With Worksheets("Data")
'Determine emptyRow in COLUMN D
emptyRow = .Cells(.Rows.Count, "D").End(xlUp).Row + 1
'Transfer information to template
.Cells(emptyRow, "D").Value = (tend - tstart) * Rnd() + tstart
'Format cell on template
.Cells(emptyRow, "D").NumberFormat = "hh:mm:ss"
End With
End Sub
RandBetween方法
要使用工作表的Randbetween函数,您需要将开始时间和结束时间转换为代表自午夜以来秒数的长整数,然后在它们之间随机化。取得结果,然后通过潜水一天中的秒数将其转换回时间值。
Private Sub Generate_Data_Button_Click2()
Dim emptyRow As Long, tstart As Long, tend As Long, siad As Long
siad = 86400 'seconds in a day
'convert string times from input to seconds in a day as Long Integer
tstart = TimeValue(start_time_textbox.Value) * siad
tend = TimeValue(end_time_textbox.Value) * siad
'Make Data Active
With Worksheets("Data")
'Determine emptyRow in COLUMN D
emptyRow = .Cells(.Rows.Count, "D").End(xlUp).Row + 1
'Transfer information to template
.Cells(emptyRow, "D").Value = Application.RandBetween(tstart, tend) / siad
'Format cell on template
.Cells(emptyRow, "D").NumberFormat = "hh:mm:ss"
End With
End Sub
答案 1 :(得分:0)
您可以作为UDF执行此操作。这个想法是生成一个随机的分钟来与Time1偏移。
首先将两个时间的小时和分钟转换为分钟为单位。
然后根据分钟数差异,使用RandBetween
生成随机分钟数,减去基准时间,然后将此随机分钟数加到基准时间(以分钟为单位)。
Function RandomTimeBetween(FromTime As Range, ToTime As Range) As Date
Dim Time1 As Date, Time2 As Date, Minutes As Integer
Time1 = CDate(FromTime.Value)
Time2 = CDate(ToTime.Value)
Minutes = Hour(Time1) * 60 + Minute(Time1) ' Base time in minutes
' Find the random offset time between them, deduct the base time
Minutes = WorksheetFunction.RandBetween(Minutes, Hour(Time2) * 60 + Minute(Time2)) - Minutes
RandomTimeBetween = Time1 + TimeSerial(0, Minutes, 0)
End Function
如果您希望随机化到秒,则需要更改为以下内容,因为TimeSerial可能会发生溢出:
Function RandomTimeBetween(FromTime As Range, ToTime As Range) As Date
Dim Time1 As Date, Time2 As Date, Seconds As Long
Time1 = CDate(FromTime.Value)
Time2 = CDate(ToTime.Value)
Seconds = Hour(Time1) * 3600 + Minute(Time1) * 60 + Second(Time1) ' Base time in seconds
' Find the random offset time between them, deduct the base time
Seconds = WorksheetFunction.RandBetween(Seconds, Hour(Time2) * 3600 + Minute(Time2) * 60 + Second(Time2)) - Seconds
RandomTimeBetween = Time1 + TimeSerial(Seconds \ 3600, Seconds \ 60, Seconds Mod 60)
End Function