我有用于运行多个操作的VBA代码。它会在运行前解锁Workbook,并在操作完成后提供保护。但是,有时由于用户打开窗口或在后台执行某些操作而导致代码出错。我希望它不显示任何类型的Visual Basic标准消息(如“编译错误”),而是在代码运行至任何类型的错误并使用ThisWorkbook.Protect Password:="123456", Structure:=True, Windows:=False
时显示我的自定义消息。
P.S。超时是由于我的计算机上的操作系统无法快速处理这些进程。
Sub TryToDoEverything()
Application.ScreenUpdating = False
ThisWorkbook.Unprotect Password:="123456"
ufProgress.LabelProgress.Width = 0
ufProgress.Show
FractionComplete (0) 'Next step
Worksheets("MAIN").Activate
Call MakeMyFolder
Application.Wait (Now + TimeValue("00:00:10"))
DoEvents
FractionComplete (0.1) 'Next step
If ThisWorkbook.Sheets("Other Data").Range("J2").Value = True Then
Call opentemplateWordOL
End If
If ThisWorkbook.Sheets("Other Data").Range("J2").Value = False Then
End If
DoEvents
FractionComplete (0.2) 'Next step
Application.Wait (Now + TimeValue("00:00:10"))
If ThisWorkbook.Sheets("Other Data").Range("J2").Value = True Then
Call opentemplateWordPL
End If
If ThisWorkbook.Sheets("Other Data").Range("J2").Value = False Then
End If
DoEvents
FractionComplete (0.4) 'Next step
Application.Wait (Now + TimeValue("00:00:10"))
FractionComplete (1) 'Next step
Worksheets("MAIN").Activate
Unload ufProgress
ThisWorkbook.Protect Password:="123456", Structure:=True, Windows:=False
TaskComplete.Show
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
根据我的经验,多次使用ThisWorkbook不好,在代码的开头使用以下内容,
dim thisWb as excel.workbook
set thisWb = thisworkbook
然后使用thisWb代替ThisWorkbook,并且用户在执行过程中单击另一个不同的wb都无关紧要。