VBA错误处理以检查文件夹是否存在

时间:2015-04-29 13:06:45

标签: excel-vba excel-2013 vba excel

我正在尝试在现有脚本中实现一些'错误处理',我将以下内容放在一起:

已编辑的代码

Private Sub Workbook_Open()

Dim j As Integer

Application.ScreenUpdating = False
'Display the splash form non-modally.
Set frm = New frmSplash
With frm
    .TaskDone = False
    .prgStatus.Value = 0
    .StartUpPosition = 0
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    .Show False
End With

For j = 1 To 1000
    DoEvents
    Next j

    iRow = 17


    fPath = "\\c\s\CAF1\Dragon Mentor Group\Dragon Scripts\Current\April 2015\"
    If fPath = vbNullString Then GoTo ErrorHandler
    If Not Dir(fPath, vbDirectory) = vbNullString Then
        On Error GoTo 0
        Set FSO = New Scripting.FileSystemObject
        frm.prgStatus.Value = 15
        If FSO.FolderExists(fPath) <> False Then
            frm.prgStatus.Value = 30
            Set SourceFolder = FSO.GetFolder(fPath)
            IsSubFolder = True
            frm.prgStatus.Value = 45
            Call DeleteRows
            frm.prgStatus.Value = 60
                Call ListFilesInFolder(SourceFolder, IsSubFolder)
                frm.prgStatus.Value = 75
            Call FormatCells
            frm.prgStatus.Value = 100
            Else
        End If
    End If
    frm.TaskDone = True
    Unload frm
    iMessage = MsgBox("The list has now been been updated!", vbOKOnly)

    '******Error Handling******
    ErrorHandler:
          MsgBox "The filepath does not exist, please contact the       administrator"
    Resume Next
    End Sub

在错误处理中,我正在尝试检查文件夹是否存在。如果没有,则发出错误消息。如果是,请运行脚本的其余部分。

问题是,即使文件路径确实存在,错误处理程序也会被激活,但我不确定为什么尽管看了很多教程。

我只是想知道是否有人可以看到这个,让我知道我哪里出错了。

2 个答案:

答案 0 :(得分:1)

全部,谢谢你的帮助和指导。我只是想让你知道,在工作同事的帮助下,我想出了如下所示的代码。非常感谢和亲切的问候。克里斯

Private Sub Workbook_Open()

    Dim j As Integer
    Dim fPath As String

    On Error GoTo errHandler:
'Display the splash form non-modally.
    Set frm = New frmSplash
    With frm
        .TaskDone = False
        .prgStatus.Value = 0
        .StartUpPosition = 0
        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
        .Show False
    End With

    For j = 1 To 1000
        DoEvents
        Next j

        iRow = 17
        fPath = "\\c\s\CAF1\Dragon Mentor Group\Dragon Scripts\Current\"
        If Dir(fPath) = "" Then
            Set FSO = New Scripting.FileSystemObject
            If FSO.FolderExists(fPath) = False Then
'' Code will run here if folder does not exist
                GoTo errHandler
            Else
                Set SourceFolder = FSO.GetFolder(fPath)
                IsSubFolder = True
                Call DeleteRows
''''''''
'' Get the max number of files in all the folders and subfolders
''''''''
                xCur = 1 'set start value for xCur here as will reset when recurssively calling ListFilesInFolder
                xMax = SourceFolder.Files.Count
                For Each SubFolder In SourceFolder.SubFolders
                    xMax = xMax + SubFolder.Files.Count
                    Next SubFolder
''
                    Call ListFilesInFolder(SourceFolder, IsSubFolder)
''
                    Call FormatCells
                End If
            End If

            frm.TaskDone = True
            Unload frm
'The row below creates a 'On Screen' message telling the user that the workbook has been built.
            MsgBox "The list has now been been updated!"
            Exit Sub
''
errHandler:
            frm.TaskDone = True
            Unload frm
            MsgBox "The file path doesn't exist, please contact the administrator", , "File Path Error"
''
        End Sub

答案 1 :(得分:0)

我会像这样解决它

On Error GoTo ErrorHandler
    fPath = "\\c\s\CAF1\Dragon Mentor Group\Dragon Scripts\Current\April 2015\"

    If Dir(fPath) <> "" Then
    End If

Set FSO = New Scripting.FileSystemObject
        frm.prgStatus.Value = 15
        If FSO.........