The VBA CurDir Function

Description

The VBA CurDir function returns the current path as a String.

The syntax of the function is:

CurDir( [Drive] )

Where the [Drive] argument is an optional String argument specifying the drive that you want to return the current path for.

If the [Drive] argument is omitted, the CurDir function returns the current path on the current drive.

Note: If you are using a Macintosh device, the [Drive] argument is ignored and the CurDir simply returns the current path on the current drive.


VBA CurDir Function Examples

The example code below shows the VBA CurDir function used to return the current paths on the C: drive and the F: drive of a PC.

' Return the current path on different drives on the current PC.
' (Note that the current drive is the C drive).
Dim path1 As String
Dim path2 As String
path1 = CurDir( )
' path1 is now equal to the String "C:\Users\John\Documents".
path2 = CurDir( "F" )
' path2 is now equal to the String "F:\Projects".

Note that in the above vba code:


VBA CurDir Function Error

If the first letter of the supplied [Drive] argument does not relate to an existing drive on the current system, you will get the error:

Run-time error '68': Device unavailable

VBA Run Time Error 68 Message Box