简体   繁体   English

Excel VBA:如何在不运行宏的情况下打开Excel文件,但希望在激活工作簿时运行宏?

[英]Excel VBA: How do I open an Excel file without running macro but want macros to run when I activate workbook?

I currently have the below sub in ThisWorkbook . 目前,我在ThisWorkbook有以下内容。 I would like to run this sub "SubName" whenever I activate this workbook, but don't want the macro to run when I open the file (it currently runs the "SubName" which causes an error because data takes about 5 seconds to load before the macro functions properly) 每当我激活此工作簿时,我都希望运行此子“ SubName”,但不希望在打开文件时运行宏(它当前运行的“ SubName”会导致错误,因为加载数据大约需要5秒钟在宏正常运行之前)

Thanks! 谢谢!

Private Sub Workbook_Activate()

SubName

End Sub

Try adding this before SubName : 尝试在SubName之前添加它:

Do Until Application.Ready = True
DoEvents: Loop

Maybe you could try the following code 也许您可以尝试以下代码

Option Explicit
Dim justOpened As Boolean

Private Sub Workbook_Activate()
    If justOpened Then
        justOpened = False
    Else
        MsgBox "Activate"
        ' Your Sub here
    End If
End Sub

Private Sub Workbook_Open()
    justOpened = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM