所以我有两张工作表,一张是“数据馈送”,另一张是“记录”。在我的数据表中有不断更新的实时数据,在记录表中每3分钟存储一次,一切都很好,但现在我想要的是为每3分钟添加一个时间,例如如果9:00的数据是100,那么我希望该时间以及我的数据都在那里,比如9:00 100,9:03 140,9:06 256等。这是我的代码,也只是想显示时间,如果你想知道我想知道单元格的时间在哪里(A2):
Vba代码:
Sub update()
With Sheets("record")
rw = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
.Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Sheets("datafeed").Range("B66").Value
.Range(.Cells(rw, 4), .Cells(rw, 4)).Value = Sheets("datafeed").Range("V66").Value
.Range(.Cells(rw, 5), .Cells(rw, 5)).Value = Sheets("datafeed").Range("B67").Value
.Range(.Cells(rw, 7), .Cells(rw, 7)).Value = Sheets("datafeed").Range("V67").Value
.Range(.Cells(rw, 8), .Cells(rw, 8)).Value = Sheets("datafeed").Range("B68").Value
.Range(.Cells(rw, 10), .Cells(rw, 10)).Value = Sheets("datafeed").Range("V68").Value
.Range(.Cells(rw, 11), .Cells(rw, 11)).Value = Sheets("datafeed").Range("B69").Value
.Range(.Cells(rw, 13), .Cells(rw, 13)).Value = Sheets("datafeed").Range("V69").Value
.Range(.Cells(rw, 14), .Cells(rw, 14)).Value = Sheets("datafeed").Range("B70").Value
.Range(.Cells(rw, 16), .Cells(rw, 16)).Value = Sheets("datafeed").Range("V70").Value
.Range(.Cells(rw, 17), .Cells(rw, 17)).Value = Sheets("datafeed").Range("B71").Value
.Range(.Cells(rw, 19), .Cells(rw, 19)).Value = Sheets("datafeed").Range("V71").Value
.Range(.Cells(rw, 20), .Cells(rw, 20)).Value = Sheets("datafeed").Range("B72").Value
.Range(.Cells(rw, 22), .Cells(rw, 22)).Value = Sheets("datafeed").Range("V72").Value
End With
Application.OnTime Now + TimeSerial(0, 3, 0), "update" ' run again after 5 minutes
End Sub发布于 2020-04-26 19:45:57
使用Time()函数将以"hh:mm:ss AM/PM"格式返回系统时间,使用Format()函数我们可以操作Time()函数的输出格式:
对于"hh:mm:ss AM/PM“格式
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Time() & " " & Sheets("datafeed").Range("B66").Value对于"hh:mm“格式(24小时时间)
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Format(time(), "hh:mm")) & " " & Sheets("datafeed").Range("B66").Value对于"hh:mm AM/PM“格式:
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Format(time(), "hh:mm AM/PM")) & " " & Sheets("datafeed").Range("B66").Value如果你只需要12小时格式的"hh:mm“,试试:
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Mid(Format(Time(), "hh:mm AM/PM"),1,5) & " " & Sheets("datafeed").Range("B66").Value如果代码可能需要一些时间来执行(超过一分钟),您可以考虑将时间值分配给代码开头的变量,并改用该变量:
Dim myTimeValue As String
myTimeValue = Mid(Format(Time(), "hh:mm AM/PM"), 1, 5)
With Sheets("record")
rw = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = myTimeValue & " " & Sheets("datafeed").Range("B66").Value
...
'The rest of your code here
End With
...
'The rest of your code herehttps://stackoverflow.com/questions/61440086
复制相似问题