vba 打开文件时的错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21840451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error handling when opening a file
提问by user2385809
I have the following code:
我有以下代码:
Sub patch()
子补丁()
Dim r As Long, j As Long, c As Long
Dim day As Long, month As Long, year As Long
year = 2014
For month = 2 To 2 Step -1
For day = 12 To 1 Step -1
On Error GoTo nextday
Workbooks.Open ("G:\Manu\PVA\Pmo_Multiprogrammes-Process & ToolsSub patch()
Dim r As Long, j As Long, c As Long
Dim myDay As Long, myMonth As Long, myYear As Long
Dim wb As Workbook
Application.DisplayAlerts = False
myYear = 2014
For myMonth = 2 To 2 Step -1
For myDay = 12 To 1 Step -1
On Error Resume Next
Set wb = Workbooks.Open("G:\Manu\PVA\Pmo_Multiprogrammes-Process & Tools##代码## GESTION DE CHANGEMENT AMéLIORER Base de données##代码## Mproduct-Suivi\Suivi du Cseries\rapport quotidien\Suivi Cseries_" & myDay & "_" & myMonth & "_" & myYear & ".xlsm")
On Error GoTo 0
If Not wb Is Nothing Then
'## Do stuff
wb.Close False
End If
Next 'day
Next 'mont
Application.DisplayAlerts = True
End Sub
GESTION DE CHANGEMENT AMéLIORER Base de données##代码## Mproduct-Suivi\Suivi du Cseries\rapport quotidien\Suivi Cseries_" & day & "_" & month & "_" & year & ".xlsm")
On Error GoTo 0
'## Do stuff
Workbooks("Suivi Cseries_" & day & "_" & month & "_" & year & ".xlsm").Close (False)
nextday:
Next 'day
Next 'mont
End Sub
When a file can not be found I just want the cofe to continue with the next file thus why I put "One Error GoTo nextday" but it does not work. I still get the file not found pop up and the the debug pop up appears too. There must be something I'm missing.
当找不到文件时,我只想让咖啡继续处理下一个文件,因此为什么我输入“One Error GoTo nextday”但它不起作用。我仍然弹出找不到文件,并且也出现调试弹出窗口。一定有什么我错过了。
回答by Dmitry Pavliv
Use this pattern:
使用这个模式:
##代码##BTW, don't use variables with specific names such as day
, month
, year
sicne in that case VBA functions with this names (DAY()
, MONTH()
, YEAR()
) won't work
顺便说一句,不要使用具有特定名称的变量,例如day
, month
, year
sicne 在这种情况下,具有此名称 ( DAY()
, MONTH()
, YEAR()
) 的VBA 函数将不起作用