常用的VBA语句与功能模块

  整理一些比较常用的VBA功能模块和语句。

  1、连接数据库

1 Set Cnn = CreateObject("ADODB.CONNECTION")
2 CnnStr = "Provider=****;Data Source=***;User ID=****;Password=****;Initial Catalog=**;Persist Security Info=True;"

  2、禁止和打开刷新,运行代码时闪屏

1 Application.ScreenUpdating = False  '禁止实时更新,禁止页面刷新
2 Application.ScreenUpdating = True '恢复实时更新,恢复页面刷新

  3、禁止和打开弹出框

Application.DisplayAlerts = False '禁止弹出框
Application.DisplayAlerts = True '恢复弹出框

  4、统计运行时间

1 tm = Now() '开始运行的时间
2 /*
3 相关代码
4 */
5 MsgBox "耗时:" & Format(Now() - tm, "s") & ""
原文地址:https://www.cnblogs.com/HYLering/p/11638734.html