嵌套在错误VBA上

时间:2016-05-11 13:45:41

标签: vba excel-vba excel

我所做的代码是在电子表格中向下两列并在文件夹中创建文件夹。左列具有第一层文件夹的名称,而第二列具有第一层文件夹中的文件夹。

1st layer   2nd layer
3508023     131392
3508023     90701 - repeat
3508023     115469
3508023     358167
3705584     90701 - repeat
3705584     133214
3705584     349453

我的代码当前处理第一层文件夹已经存在,跳过它并进入下一次迭代,但如果第二层文件夹已经通过嵌套错误语句存在,我无法弄清楚如何处理错误。 / p>

这是我的代码

Sub ProPlanner()
    '
    ' ProPlanner Macro
    '
    Dim strRout As String
    Dim strTask As String
    Dim strPath As String

    Dim RoutCellCol As Integer
    Dim TaskCellCol As Integer

    Dim RoutCellRow As Integer
    Dim TaskCellRow As Integer

    Dim NewRoutPath As String
    Dim NewTaskPath As String

    'declare staring cells in spreadsheet

    RoutCellCol = 2
    RoutCellRow = 2

    TaskCellCol = 6
    TaskCellRow = 2


    Do Until TaskCellRow > 72 'ending row

    strRout = Cells(RoutCellRow, RoutCellCol).Value

    On Error GoTo TaskInRoute

    'create first layer folder in shared file

    NewRoutPath = ("S:DEPT\IE\Employee Files\Chris\" & "Rout " & strRout)
    MkDir (NewRoutPath)

    'create the first 2nd layer folder within the 1st layer folder just created

    strTask = Cells(TaskCellRow, TaskCellCol).Value

    NewTaskPath = (NewRoutPath & "\" & strTask)
    MkDir (NewTaskPath)


IterationLoop:

    RoutCellRow = RoutCellRow + 1
    TaskCellRow = TaskCellRow + 1

Loop

Exit Sub

'error handler if 1st layer folder already exists

TaskInRoute:


    strTask = Cells(TaskCellRow, TaskCellCol).Value

    NewTaskPath = (NewRoutPath & "\" & strTask)
    MkDir (NewTaskPath) 'error occurs here if 2nd layer folder already exists...nested on error statement?



   Resume IterationLoop


Exit Sub

End Sub

1 个答案:

答案 0 :(得分:1)

你冷添加支票如下

TaskInRoute:

    strTask = Cells(TaskCellRow, TaskCellCol).Value

    NewTaskPath = (NewRoutPath & "\" & strTask)
    If Dir(NewRoutPath & "\" & strTask, vbDirectory) = "" Then
        MkDir (NewTaskPath) 'error occurs here if 2nd layer folder already exists...nested on error statement?
    Else
        ' subfolder already exists
        ' do other things
    End If

    Resume IterationLoop