ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 389|回复: 0

[求助] 如何解决OnTime函数越用越慢的问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-4-18 14:24 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
在做数据大屏幕的时候,想用OnTime这个函数,做定时数据的刷新(几秒更新一次), 包括倒计时(每秒刷新一次),代码如下!
程序里面,一共用了五个OnTime,有些数据是一秒刷新一次,有些是3秒,还有10秒,还有60秒刷新的。

但是遇到一个问题,就是这个程序刚开始的时候,执行没问题,但执行几十分钟后开始越来越慢,几小时后,经常就会卡住,更本不可能一秒一更新,有时间会好几秒才能更新一次! 请问这是哪里出问题了?应该怎么优化下?

'每10秒钟刷新一次数据

Sub ZuoShang()

'左上角订单刷新
If Time() > CDate(Sheet2.Range("A" & DingDan)) Then

    ActiveSheet.Shapes.Range(Array("Picture 5")).Select  '更新左上角订单
        Selection.Formula = "=Sheet2!A" & DingDan - 6 & ":D" & DingDan  '更新左上角订单
    ActiveSheet.Shapes.Range(Array("Picture 11")).Select  '取消选择

    DingDan = DingDan + 1
    Sheet3.Range("E12") = DingDan '更新今日引流数量
    Sheet3.Range("E14") = Sheet3.Range("E14") + 1 '更新总引流量
End If

Application.OnTime Now + TimeValue("00:00:10"), "ZuoShang"  '10秒刷新一次左上角

End Sub


Sub ZhongJian()

Sheet6.Range("C1") = Sheet6.Range("C1") + Int(100 * Rnd() + 66) '刷新中间大数字

Sheet3.Range("L1") = Now() '更新当前时间

Application.OnTime Now + TimeValue("00:00:01"), "ZhongJian"  '1秒刷新一次中间大数据
End Sub

Sub YouXia()

'右下角排名滚动
Sheet4.Range("A1:D1").Copy Sheet4.Range("A21:D21")
Sheet4.Rows(1).Delete
ActiveSheet.Shapes.Range(Array("Picture 17")).Select '更新右下角排名
    Selection.Formula = "=Sheet4!A1:D5" '更新右下角排名
ActiveSheet.Shapes.Range(Array("Picture 11")).Select '取消选择

Application.OnTime Now + TimeValue("00:00:03"), "YouXia"  '3秒刷新一次右下数据
End Sub

Sub ZuoZhong()

Sheet3.Range("B" & JinTian & ":B" & JinTian + Hour(Now())).Copy Sheet3.Range("J2:J" & Hour(Now()) + 2) '刷新左中业绩折线图

Application.OnTime Now + TimeValue("00:01:00"), "ZuoZhong"  '1分钟刷新一次折线图
End Sub


Sub ZheXianYeJi()
'左中业绩曲线图滚动报数
Select Case YeJiXunHuan
    Case 0
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M3:N5"
        Selection.ShapeRange.Top = 450
        Selection.ShapeRange.Left = 63
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 1
    Case 1
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M6:N8"
        Selection.ShapeRange.Top = 475
        Selection.ShapeRange.Left = 76
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 2
    Case 2
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M9:N11"
        Selection.ShapeRange.Top = 490
        Selection.ShapeRange.Left = 89
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 3
    Case 3
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M12:N14"
        Selection.ShapeRange.Top = 500
        Selection.ShapeRange.Left = 102
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 4
    Case 4
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M15:N17"
        Selection.ShapeRange.Top = 500
        Selection.ShapeRange.Left = 115
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 5
    Case 5
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M18:N20"
        Selection.ShapeRange.Top = 502
        Selection.ShapeRange.Left = 128
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 6
    Case 6
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M21:N23"
        Selection.ShapeRange.Top = 505
        Selection.ShapeRange.Left = 141
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 7
    Case 7
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M24:N26"
        Selection.ShapeRange.Top = 485
        Selection.ShapeRange.Left = 154
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 8
    Case 8
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M27:N29"
        Selection.ShapeRange.Top = 482
        Selection.ShapeRange.Left = 167
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 9
    Case 9
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M30:N32"
        Selection.ShapeRange.Top = 465
        Selection.ShapeRange.Left = 180
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 10
    Case 10
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M33:N35"
        Selection.ShapeRange.Top = 460
        Selection.ShapeRange.Left = 193
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 11
    Case 11
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M36:N38"
        Selection.ShapeRange.Top = 450
        Selection.ShapeRange.Left = 206
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 12
    Case 12
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M39:N41"
        Selection.ShapeRange.Top = 445
        Selection.ShapeRange.Left = 219
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 13
    Case 13
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M42:N44"
        Selection.ShapeRange.Top = 430
        Selection.ShapeRange.Left = 232
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 14
    Case 14
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M45:N47"
        Selection.ShapeRange.Top = 410
        Selection.ShapeRange.Left = 245
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 15
    Case 15
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M48:N50"
        Selection.ShapeRange.Top = 415
        Selection.ShapeRange.Left = 258
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 16
    Case 16
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M51:N53"
        Selection.ShapeRange.Top = 420
        Selection.ShapeRange.Left = 271
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 17
    Case 17
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M54:N56"
        Selection.ShapeRange.Top = 425
        Selection.ShapeRange.Left = 284
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 18
    Case 18
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M57:N59"
        Selection.ShapeRange.Top = 430
        Selection.ShapeRange.Left = 297
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 19
    Case 19
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M60:N62"
        Selection.ShapeRange.Top = 432
        Selection.ShapeRange.Left = 310
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 20
    Case 20
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M63:N65"
        Selection.ShapeRange.Top = 425
        Selection.ShapeRange.Left = 323
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 21
    Case 21
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M66:N68"
        Selection.ShapeRange.Top = 420
        Selection.ShapeRange.Left = 336
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 22
    Case 22
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M69:N71"
        Selection.ShapeRange.Top = 425
        Selection.ShapeRange.Left = 349
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 23
    Case 23
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        Selection.Formula = "=Sheet3!M72:N74"
        Selection.ShapeRange.Top = 422
        Selection.ShapeRange.Left = 362
        ActiveSheet.Shapes.Range(Array("Picture 11")).Select
        YeJiXunHuan = 0

End Select


Application.OnTime Now + TimeValue("00:00:03"), "ZheXianYeJi"  '启动每秒刷新
End Sub


Sub 宏8()

ZhongJian
ZuoShang
YouXia
ZuoZhong
ZheXianYeJi

End Sub



Sub 宏4()
Application.OnTime Now + TimeValue("00:00:01"), "ZhongJian", , False '1秒刷新一次中间大数据
Application.OnTime Now + TimeValue("00:00:10"), "ZuoShang", , False '10秒刷新一次左上角
Application.OnTime Now + TimeValue("00:00:03"), "YouXia", , False '3秒刷新一次右下数据
Application.OnTime Now + TimeValue("00:01:00"), "ZuoZhong", , False '1分钟刷新一次折线图
Application.OnTime Now + TimeValue("00:00:03"), "ZheXianYeJi", , False '启动每秒刷新

End Sub


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 14:46 , Processed in 0.034962 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表