save as to specific directory (different paths)

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
I need to hit a button. It then saves the file with the name I choose to a directory that could be different on each different computer, but the last part of the path is always the same "\dropbox\clients\".

Is this possible to find the wildcard of a path using code?

Something like:
Code:
If Dir(DBPath, vbDirectory) <> "" Then
                ActiveWorkbook.SaveAs DBPath & myFileName
DBPath = ?? Whatever wildcard code we can create
myFileName = Worksheets("Estimating").Range("A10").Value & ".xls"

Even if It could bring up the dialog box to save the file, that would be great. Then they can HIT ok to make sure it is going to the right place.

Thank You,
Michael D
 
They might have:
C:\Documents\Bob\Dropbox\Clients\
or
C:\My Documents\Dave\Dropbox\Clients
or
C:\Dropbox\Clients

So this is why using the wildcard would be nice to find the proper path.

Thyank You
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this:

Code:
Function GetDir(ByVal sRoot As String, sPath As String) As String
    ' Returns a folder matching the pattern
    ' sRoot & "*" & sPath
    
    ' E.g., sRoot = "C:\", sPath ="\dropbox\clients"
    ' gives the pattern "C:\*\dropbox\clients"
    ' Requires a reference to Microsoft Scripting Runtime
    Dim oFSO        As Scripting.FileSystemObject
    Dim col         As Collection               ' working collection of folders
    Dim oSub        As Scripting.Folders        ' subfolders in a given folder
    Dim oFld        As Scripting.Folder         ' one folder among subfolders
    Dim sPatt       As String
 
    Set oFSO = New Scripting.FileSystemObject
    If Not oFSO.FolderExists(sRoot) Then Exit Function
 
    sPatt = LCase(sRoot & "*" & sPath & "*")
    Set col = New Collection
    Set oFSO = New Scripting.FileSystemObject
    col.Add sRoot
    
    ' for hidden folders:
    On Error Resume Next
 
    Do While col.Count
        Set oSub = oFSO.GetFolder(col(1)).SubFolders
        If oSub.Count Then
            If Err.Number Then
                ' hidden folder -- let it go
                Err.Clear
            Else
                For Each oFld In oSub
                    If LCase(oFld.Path) Like sPatt Then
                        GetDir = oFld.Path
                        Exit Function
                    End If
                    col.Add oFld.Path
                Next oFld
            End If
        End If

        col.Remove 1
    Loop
End Function

E.g., (as a UDF),

=GetDir("C:\", "dropbox\clients")

Tightening up the root (e.g, "C:\Documents and Setting\username") would make it faster.
 
Upvote 0
Any way to do this as code instead of a user defined function.
A User Defined Function is code, and can be used/called in VBA code like other functions available in VBA.
 
Upvote 0
OK Great!
How?
HOw can I put this UDF in my code?
I want to open a dialog box in the path dropbox\clients

Thank You,
Michael
 
Upvote 0
Tried the UDF and get an error on this line:
Dim oFSO As Scripting.FileSystemObject

I am sure I need to add scripting,How do I do that?

Michael
 
Upvote 0
Tools > References, locate and tick Microsoft Scripting Runtime.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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