Article ID: 108351
Article Last Modified on 6/23/2005
ChDir "C:"and you will receive the following error message: The following lines of code will succeed, because they each contain a backslash:
ChDir "C:\"
-or-
ChDir "C:\EXCEL\LIBRARY"It is possible to incorporate error-handling into a ChDir function to prevent an error from occurring. The Visual Basic code example below demonstrates one way to do this.
'----------------------------------------------------------------------
Option Explicit
Sub MainMacro()
'Dimension some variables.
Dim NewDir As Variant
'Prompts you to enter a drive/directory. For example, D: or C:\EXCEL.
'If you type in an invalid directory, the subroutine will fail. For
'example, typing just "C" (without the quotes) is not going to work.
NewDir = InputBox("Switch to which drive/directory?")
'If the NewDir ends in a colon, indicating it is a root directory,
'concatenate a backslash onto the end of it. For example, C: would
'become C:\. If you actually enter "C:\", the subroutine doesn't add
'another backslash.
If Right(NewDir, 1) = ":" Then
NewDir = NewDir & "\"
End If
'Switches to the proper drive (the first letter in NewDir: 'Left'
'gives us this) and directory.
ChDrive Left(NewDir, 1) 'change to the drive (C:, etc.)
ChDir NewDir 'change to the directory
'Display the name of the current directory so you know it worked.
MsgBox "Current directory is " & CurDir()
End Sub
'----------------------------------------------------------------------
The If-End If routine is the error-checking procedure. Whenever you use a
ChDir statement, preceding it with the If-End If routine (or some variation
of it) can help prevent the "no backslash" error.
Additional query words: 8.00 97 Run-time error 76 Path not found XL
Keywords: kberrmsg kbinfo kbprogramming KB108351