VBA代码中途重启而没有错误消息

时间:2015-07-20 12:24:47

标签: excel vba excel-vba error-handling

我有下面的代码似乎工作正常但是在它的中途,它在我调试代码时停止并重新启动。

重新启动时未显示任何错误消息。有谁知道我的代码为什么会这样做?

由于 SB

Sub ExportPICs()

If FileIsOpen("U:\FBS\PROJECTS_TEAM\MI\Sean's Projects\PICS and Benefits\Pics & Benefits upload file.xlsm") Then
    MsgBox "That file's open, or doesn't exist - do something else."
    Exit Sub
    End If

Sheets("Post Implementation Costs").Visible = True
Sheets("Post Implementation Costs").Activate
Sheets("Post Implementation Costs").Unprotect Password:="Projects123"

Dim Tracker As String
Tracker = ThisWorkbook.Name

Dim wkbk As Excel.Workbook
Set wkbk = Workbooks.Open(Filename:= _
"U:\FBS\PROJECTS_TEAM\MI\Sean's Projects\PICS and Benefits\Pics & Benefits upload file.xlsm") '***********CODE RESTARTS HERE WITHOUT ERROR MESSAGE

Dim pics As String
pics = Dir("U:\FBS\PROJECTS_TEAM\MI\Sean's Projects\PICS and Benefits\Pics & Benefits upload file.xlsm")


Workbooks(Tracker).Activate
Sheets("Post Implementation Costs").Activate

Dim rng As Range
Dim CountTrue As Long
Set rng = Range("D16:D100")
CountTrue = Application.WorksheetFunction.CountIf(rng, "PIC")

If CountTrue > 0 Then
Rows("19:" & (CountTrue + 18)).EntireRow.Copy
End If

End Sub

2 个答案:

答案 0 :(得分:0)

我也发现,对于某些语句,Step命令不会执行,但允许以下所有代码运行。为避免这种情况,请在步骤混淆的后面一行设置断点。

答案 1 :(得分:0)

如果您在" Pics&amp ;;中的工作簿公开活动中有任何代码好处上传file.xlsm"这会做循环。

尝试Application.EnableEvents = False

befor

Dim wkbk As Excel.Workbook
Set wkbk = Workbooks.Open(Filename:= _
"U:\FBS\PROJECTS_TEAM\MI\Sean's Projects\PICS and Benefits\Pics & Benefits upload file.xlsm")

然后你可以再次激活它。

Application.EnableEvents = True

此代码停止调用Excel事件处理程序。将其设置为false通常是因为事件处理程序的效果是不合需要的,或者是为了防止无限循环。