Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

Thread: ChDir error handling

  1. #1
    Guest

    Default

    Could you explain how can i handle ChDir errors in a program?
    My codes is like

    On Error GoTo noFolder
    ChDir CurFolder & "" & category

    The folder: CurFolder & "" category does not exist. The system pops up a message telling me the path not found instead of going to error handling line noFolder:. So how can fix this problem?
    Thx in advance

  2. #2
    New Member
    Join Date
    Mar 2002
    Posts
    15
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    your code must be skipping past your error handling.
    this works fine:

    Sub errtest()
    On Error GoTo noFolder
    ChDir CurFolder & "\" & Category

    noFolder:
    If Err.Number = 76 Then
    MsgBox "Can't go there !!"
    End If
    End Sub

  3. #3
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-03-13 16:24, Anonymous wrote:
    Could you explain how can i handle ChDir errors in a program?
    My codes is like

    On Error GoTo noFolder
    ChDir CurFolder & "" & category

    The folder: CurFolder & "" category does not exist. The system pops up a message telling me the path not found instead of going to error handling line noFolder:. So how can fix this problem?
    Thx in advance
    What do you want to do if the folder does not exist?

  4. #4
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    You can use sutjh's error 76 method. Or you could use the Dir function to check for the folder.

    Here is a little example which doesn't have an actual error handler, but may give you some ideas.

    -rh

    Code:
    Sub TestChDir()
    
        Dim fPathOK As Boolean
        Dim curFolder, Category
        Dim curPath As String
        
        On Error Resume Next
        Do While Not fPathOK
            ChDir InputBox("Enter a path to change to")
            If Err = 0 Then
                ChDir curPath
                fPathOK = True
            Else
                MsgBox "your path was not found, try again"
                Err.Clear
            End If
        Loop
        
        On Error GoTo 0
    
    End Sub
    [ This Message was edited by: Russell Hauf on 2002-03-13 17:56 ]

  5. #5
    Guest

    Default

    Thx. Two procedures you provided run well. I think my code should also work. Still don't the reason of malfunction. Also i have some doubts in On Error GoTo 0.
    Can this statement be used twice in one procedure? eg.
    Code:
    On error goto errHandling1:
    'Action
    on Error goto 0
    on error goto errorHandling2:
    'Other action
    on Error goto 0
    Can this work? i don't know whether all error handlings in the procedures are disabled after first occurence of "on Error goto 0". From help files, seems so. But what if i do need different error handlings in the same procedure? what's the functional scope on error ...?

  6. #6
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-03-14 01:32, Anonymous wrote:
    Thx. Two procedures you provided run well. I think my code should also work. Still don't the reason of malfunction. Also i have some doubts in On Error GoTo 0.
    Can this statement be used twice in one procedure? eg.
    Code:
    On error goto errHandling1:
    'Action
    on Error goto 0
    on error goto errorHandling2:
    'Other action
    on Error goto 0
    Can this work? i don't know whether all error handlings in the procedures are disabled after first occurence of "on Error goto 0". From help files, seems so. But what if i do need different error handlings in the same procedure? what's the functional scope on error ...?
    You can use one error handler to handle all cases (example below). On Error Goto 0 just re-sets the error handling to have Excel handle your errors. If you have an "On Error Resume Next" line, and want to switch back to your error handler, you do not need to have the goto 0 line first.

    Now, for that error handler:
    Code:
    '...some code
    ExitHere:
        Exit Sub
    
    HandleErr:
        Select Case Err.Number
        Case 76
            ' do something to handle "Path not found"
        Case 55
            ' do something about error 55
        Case Else
            ' for all other errors, throw up
            ' Excel's error message and exit.
            MsgBox Err.Description
            Resume ExitHere
        End Select
    End Sub
    Hope this gives you an idea. You should not need multiple error handlers in a procedure.

    -rh

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •