希望有人帮我解决这个问题。
我有一个子例程,可以将我的数据从此工作簿复制到另一个现有工作簿(Report Workbook)。
当我尝试运行代码时,在运行期间打开报告工作簿后,它突然停止,我注意到它进入了设计模式。因此,妨碍了例程的执行。
但是当我试图在子程序上设置一个断点并再次继续它时。它执行没有问题。这看起来有点奇怪。
我有点想要解决这个问题。
到目前为止,这是我尝试过的故障排除步骤。
但仍然没有运气。它会阻止代码执行。
Private Sub TransferRawData()
Dim wsPTRawData As Worksheet, wbPTWorkBook As Workbook, wsOutputRaw As Worksheet
Dim filePath As String, FileName As String, ptTargetRow As Long
Application.EnableEvents = False
Application.StatusBar = "Exporting All Raw Data... Please wait a moment..."
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
filePath = ThisWorkbook.Path & "\"
FileName = filePath & pt_FileName
Set wbPTWorkBook = Workbooks.Open(FileName:=FileName, UpdateLinks:=0, IgnoreReadOnlyRecommended:=True)
DoEvents
Set wsPTRawData = wbPTWorkBook.Worksheets(pt_ProdRawSheet)
Set wsOutputRaw = ThisWorkbook.Sheets(merger_prodOutputSheet)
ptTargetRow = wsPTRawData.Range("A" & Rows.Count).End(xlUp).Row + 1
If lastRow(wsOutputRaw, "A") > 1 Then wsOutputRaw.Range("A2:F" & lastRow(wsOutputRaw, "A")).Copy wsPTRawData.Range("A" & ptTargetRow)
wbPTWorkBook.Close True
Set wsOutputRaw = Nothing
Set wsPTRawData = Nothing
Set wbPTWorkBook = Nothing
End Sub
顺便说一下,我有3套使用上述代码的工作簿。所有这些都将复制报告工作簿中的数据。但是他们都在执行转移例程时遇到了麻烦。
了解更多信息。一旦我运行例程并执行Workbooks.Open事件。以下是VBA项目窗口和Excel窗口的屏幕截图。
答案 0 :(得分:1)
在此处添加错误处理程序,看看是否有任何错误未被正确捕获
Private Sub TransferRawData()
Dim wsPTRawData As Worksheet, wbPTWorkBook As Workbook, wsOutputRaw As Worksheet
Dim filePath As String, FileName As String, ptTargetRow As Long
Application.EnableEvents = False
Application.StatusBar = "Exporting All Raw Data... Please wait a moment..."
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
filePath = ThisWorkbook.Path & "\"
FileName = filePath & pt_FileName
On Error GoTo ErrHandler
Set wbPTWorkBook = Workbooks.Open(FileName:=FileName, UpdateLinks:=0, IgnoreReadOnlyRecommended:=True)
DoEvents
Set wsPTRawData = wbPTWorkBook.Worksheets(pt_ProdRawSheet)
Set wsOutputRaw = ThisWorkbook.Sheets(merger_prodOutputSheet)
ptTargetRow = wsPTRawData.Range("A" & Rows.Count).End(xlUp).Row + 1
If lastRow(wsOutputRaw, "A") > 1 Then wsOutputRaw.Range("A2:F" & lastRow(wsOutputRaw, "A")).Copy wsPTRawData.Range("A" & ptTargetRow)
wbPTWorkBook.Close True
Set wsOutputRaw = Nothing
Set wsPTRawData = Nothing
Set wbPTWorkBook = Nothing
ErrHandlerSave:
Debug.Print Chr(13) & "Error # " & Str(Err.Number) & " was generated on Saving" _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
End Sub
您将在VBA代码底部的Immediate
窗口中看到错误消息。如果您不了解或不确定如何展示LOOK HERE
答案 1 :(得分:0)
嗨参考其他人将来会遇到这个问题。
代码本身与错误没有任何关系。
经过大量时间调查此错误。我发现罪魁祸首是我在宏本身上分配的快捷键。
我在宏上分配了一个 CTRL + SHIFT 键,从而阻碍了其开放的其他工作簿的其他宏。
接下来,我在Macro快捷键上进行了配置,并避免使用 Shift 键。结果,子程序做了它应该做的事情。