![]() |
|
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
Join Date: Mar 2002
Posts: 15
|
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 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,368
|
Quote:
|
|
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,368
|
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
|
|
|
|
|
|
#5 |
|
Guest
Posts: n/a
|
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 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,368
|
Quote:
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
-rh |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|