On Error doesn't skip error handling and Chdir() doesn't change directories in VBA

23 Views Asked by At

I have two questions: 1) why is this simple code not changing directories? And, 2)Why doesn't my On Error code jump to the goto label? I was under the impression that the code between the Error label (ie DrawsError1:) and the On Error goto 0 statement would not be run unless there was an error. Is this wrong? I only want test set to NA when there is an error.

The below listed code is what I have tried. I am trying to check a network drive to see if the folder "Draws" exist. If it doesn't, I want to create it in the same location on the network drive. If it does exist, then I want to incrementally look for folders marked "Draw 1", "Draw 2"... within "Draws", determine the highest number Draw folder that exist and make the next folder such as "Draw 3".

Any guidance would be appreciated.

Tom

Sub RunDraw()
    
    Dim Answer As Variant, Page As Range, test As String, PageCount As Integer, COStart As String, COPage As Range, COSumPrevious As Currency, _
        COSumNew As Currency, OrigSum As Currency, COTotalSum As Currency, EFGProp As Variant, NCSProp As Variant, G702Name As String, A As Integer, _
        Drawcount As Integer
    
    Application.ScreenUpdating = False
    
    ChDir ("\\TSRS\tsrs\jobs\")                            'This is the Chdir command that doesn't work.
    MsgBox(Your current directory is: " & CurDir)          'It should put me in a network drive but instead 
                                                           'it puts me in C:\Users\Tom\Documents
 '   Call ContinuationID(Answer, EFGProp, NCSProp)  'Unrelated subroutine
    
 '   If Answer = 1 Then
 '       Exit Sub            'Sub exit from previously called unrelated subroutine
 '   End If
    
    test = ""
    Drawcount = 0
    
    On Error GoTo DrawsError1:
    ChDir CurDir & "\Draws"
    
DrawsError1:
    test = "NA"
    On Error GoTo 0
    
    If test <> "NA" Then
        For A = 1 To 100
            On Error GoTo DrawsError2:
            ChDir CurDir & "\Draw " & Trim(Str(A))
            test = A
            Drawcount = A
            ChDir CurDir & "\Draws"
DrawsError2:
        Exit For
        On Error GoTo 0
        Next A

        Else
        If test = "NA" Then
            G702Name = "Draw 1"
            MkDir (CurDir & "\Draws")
            MkDir (CurDir & "\Draws/Draw 1")
            Drawcount = 1
        Else
            G702Name = "Draw " & Trim(Str(A))
            MkDir (CurDir & "\Draws\Draw " & Trim(Str(test + 1)))
            Drawcount = test + 1
        End If
    End If
End Sub

The listed code is what I have tried. I am trying to check a network drive to see if the folder "Draws" exist. If it doesn't, I want to create it in the same location on the network drive. If it does exist, then I want to incrementally look for folders marked "Draw 1", "Draw 2"... within "Draws", determine the highest number Draw folder that exist and make the next folder such as "Draw 3".

0

There are 0 best solutions below