首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单元格值赋值中包括时间间隔为3分钟

在单元格值赋值中包括时间间隔为3分钟
EN

Stack Overflow用户
提问于 2020-04-26 19:40:19
回答 1查看 47关注 0票数 0

所以我有两张工作表,一张是“数据馈送”,另一张是“记录”。在我的数据表中有不断更新的实时数据,在记录表中每3分钟存储一次,一切都很好,但现在我想要的是为每3分钟添加一个时间,例如如果9:00的数据是100,那么我希望该时间以及我的数据都在那里,比如9:00 100,9:03 140,9:06 256等。这是我的代码,也只是想显示时间,如果你想知道我想知道单元格的时间在哪里(A2):

Vba代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-26 19:45:57

使用Time()函数将以"hh:mm:ss AM/PM"格式返回系统时间,使用Format()函数我们可以操作Time()函数的输出格式:

对于"hh:mm:ss AM/PM“格式

代码语言:javascript
复制
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Time() & " " & Sheets("datafeed").Range("B66").Value

对于"hh:mm“格式(24小时时间)

代码语言:javascript
复制
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Format(time(), "hh:mm"))  & " " & Sheets("datafeed").Range("B66").Value

对于"hh:mm AM/PM“格式:

代码语言:javascript
复制
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Format(time(), "hh:mm AM/PM"))  & " " & Sheets("datafeed").Range("B66").Value

如果你只需要12小时格式的"hh:mm“,试试:

代码语言:javascript
复制
Range(.Cells(rw, 2), .Cells(rw, 2)).Value = Mid(Format(Time(), "hh:mm AM/PM"),1,5) & " " &  Sheets("datafeed").Range("B66").Value

如果代码可能需要一些时间来执行(超过一分钟),您可以考虑将时间值分配给代码开头的变量,并改用该变量:

代码语言:javascript
复制
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 here
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61440086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档