ChDir error handling

G

Guest

Guest
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
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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 ...?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,220
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top