Excel 基于当前时间运行宏
请查看随附的屏幕截图,以便于参考 “AC”列中的LTP是指股票的最后交易价格LTP每秒更改一次,并自动从其他软件导入Excel 基于当前时间运行宏,excel,vba,Excel,Vba,请查看随附的屏幕截图,以便于参考 “AC”列中的LTP是指股票的最后交易价格LTP每秒更改一次,并自动从其他软件导入 列AF1:时间自动更新 AQ列:根据以下代码进行更新 AQ列使用的代码也应该用于AS列,但我面临一个问题 列为:当时间为上午9:15时。应根据AC列中的价格更新整个列 专栏:只要时间是上午9:30。应根据AC列中的价格更新整个列 请在这个问题上帮助我,如果问题的结构不正确,请原谅 If ActiveSheet.AutoFilterMode = True Then Activ
- 列为:当时间为上午9:15时。应根据AC列中的价格更新整个列
- 专栏:只要时间是上午9:30。应根据AC列中的价格更新整个列
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
Range("AC3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AQ3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
[AQ:AQ].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
Rows("2:2").Select
Range("A1").Activate
Selection.AutoFilter
花了我一段时间,但它来了: 首先,您需要将此代码插入
此工作簿
模块:
Option Explicit
Private Sub Workbook_Open()
Starting Time
End Sub
然后插入新模块并粘贴此代码:
Option Explicit
Private Const TimeFrame As Byte = 15 'this will let you edit how many minutes you want between events
Sub Starting(CurrentTime As Date)
'This macro controls when the update will be fired.
Dim StartTime As Date: StartTime = "09:15" 'Starting Time
Dim EndTime As Date: EndTime = "15:30" 'End time
Dim NextFrame As Date: NextFrame = TimeSerial(Hour(CurrentTime), Application.RoundUp(Minute(CurrentTime) / TimeFrame, 0) * TimeFrame, 0)
If NextFrame < StartTime Then
Application.OnTime StartTime + TimeSerial(0, 1, 0), "UpdateValuesForTime"
ElseIf NextFrame > EndTime Then
Exit Sub
Else
Application.OnTime NextFrame + TimeSerial(0, 1, 0), "UpdateValuesForTime"
End If
'The "+ TimeSerial(0, 1, 0)" is to start the procedure 1 minute after the frame for if it takes a while to update the values.
End Sub
Sub UpdateValuesForTime()
Dim CurrentTime As Date
CurrentTime = TimeSerial(Hour(Time), (Application.RoundUp(Minute(Time) / TimeFrame, 0) * TimeFrame) - TimeFrame, 0)
'Turn Time to String like in your sheet to find the column
Dim StrTime As String: StrTime = Chr(34) & Format(CurrentTime, "hh:mm") & Chr(34)
With ThisWorkbook.Sheets("YourSheetName") 'change this name
'Find last row for column AC
Dim LastRow As Long: LastRow = .Cells(.Rows.Count, "AC").End(xlUp).Row
'Find correct column depending on your time
Dim Col As Long: Col = .Rows(2).Find(StrTime).Column
'Paste values from column AC to the column which time fits.
.Range(.Cells(3, Col), .Cells(LastRow, Col)).Value = .Range("AC2:AC" & LastRow).Value
End With
'Call the starting macro again to schedule the next event
Starting Time
End Sub
选项显式
Private Const TimeFrame As Byte=15'这将允许您编辑事件之间所需的分钟数
子启动(当前时间为日期)
'此宏控制何时启动更新。
Dim StartTime As Date:StartTime=“09:15”'开始时间
Dim EndTime As Date:EndTime=“15:30”结束时间
Dim NextFrame As Date:NextFrame=时间序列(小时(当前时间),应用程序。汇总(分钟(当前时间)/时间帧,0)*时间帧,0)
如果下一帧<开始时间,则
Application.OnTime StartTime+时间序列(0,1,0),“UpdateValuesForTime”
ElseIf NextFrame>EndTime然后
出口接头
其他的
Application.OnTime NextFrame+时间序列(0,1,0),“UpdateValuesForTime”
如果结束
'如果更新值需要一段时间,“+时间序列(0,1,0)”将在帧后1分钟开始该过程。
端接头
子UpdateValuesForTime()
将当前时间变为日期
CurrentTime=时间序列(小时(时间),(应用程序汇总(分钟(时间)/时间段,0)*时间段)-时间段,0)
'将时间转换为工作表中的字符串以查找列
作为字符串的Dim StrTime:StrTime=Chr(34)和Format(CurrentTime,“hh:mm”)和Chr(34)
使用ThisWorkbook.Sheets(“YourSheetName”)'更改此名称
'查找列AC的最后一行
将LastRow的长度调整为:LastRow=.Cells(.Rows.Count,“AC”).End(xlUp).Row
'根据您的时间查找正确的列
将列的长度调整为:列=.Rows(2).Find(StrTime).Column
'将值从列AC粘贴到适合时间的列。
.Range(.Cells(3,Col),.Cells(LastRow,Col)).Value=.Range(“AC2:AC”和LastRow).Value
以
'再次调用启动宏以安排下一个事件
开始时间
端接头
只需在9:15到15:30之间打开工作簿,就可以实现这一点。您将看到,如果需要,开始和结束时间很容易更改。还记得更改宏运行的工作表名称。我建议您使用Windows任务计划程序来控制执行 您只需在
工作簿上打开一个宏
Private Sub Workbook_Open()
Dim lngUsedCol As Long
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
Range(Range("AC3"), Range("AC3").End(xlDown)).Copy
Range("AQ3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.AutoFilterMode = False
lngUsedCol = ActiveSheet.UsedRange.Columns.Count + 1
ActiveSheet.Cells(2, lngUsedCol) = Format(Now(), "hh:mm:ss")
Range(Range("AC3"), Range("AC3").End(xlDown)).Copy
ActiveSheet.Cells(3, lngUsedCol).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
任务计划程序将根据预先配置的计划打开文件。有了这种方法,就不需要人工干预
使用此链接查看如何使用任务计划程序计划任务-您可以使用
工作簿\u Open()
事件手动触发每个计划(给出所需的确切时间),或者如果计划每15分钟执行一次,持续X小时,在开始时间触发事件,然后您的主宏将每隔15分钟触发一次事件,直到X小时。@Damian请您提供一个工作示例,说明您的答案。我是VBA新手,非常感谢您的帮助。您能给出您希望宏工作的时间框架吗?从X到Y每隔15分钟?@Damian交易于09:15开始,15:30结束。因此,我需要从09:15到15:30每15分钟一次,你错过了这里的要点,AQ3
列是给定时间的,例如09:15,但这需要根据时间进行更改。我认为必须首先将数据复制到AQ
即Mod LT,然后复制到相应的时间列。在上述方法中,用户不必输入时间作为列标题。脚本本身会根据触发的时间更新列名。很抱歉,我已停用了几天。非常感谢你。你节省了很多时间和精力。继续成长。