VBA: How to save a new workbook in a specific location?

sharonng

New Member
Joined
Jul 14, 2021
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
I am trying to add and save a new workbook to a specific location. However, the code below does not function properly, i.e. no new workbook is created and saved in the drive. Please help.
VBA Code:
Option Compare Database
Option Explicit

Public Function DirPicker(Optional ByVal strWindowTitle As String = "Select Location to Save") As String
    Dim fd As Office.FileDialog

    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
   
    fd.Title = strWindowTitle
    If fd.Show = -1 Then
        DirPicker = fd.SelectedItems(1)
    Else
        DirPicker = vbNullString
    End If
    Set fd = Nothing
End Function

Private Sub Export_2_Click()

Dim sPath As String

sPath = DirPicker()
   
    If Len(sPath) = 0 Then
        If MsgBox("Do you want to save it in your Documents?", vbQuestion + vbYesNo) = vbYes Then
            sPath = Application.CurrentProject.Path
            Workbooks.Add.SaveAs FileName:=sPath & "Aggregated Files.xlsx"
        Else
            sPath = Application.CurrentProject.Path
            Workbooks.Add.SaveAs FileName:=sPath & "Aggregated Files.xlsx"
        End If
    End If
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
It is solved.
Code as follows:
VBA Code:
Option Compare Database
Option Explicit

Public Function DirPicker(Optional ByVal strWindowTitle As String = "Select Location to Save") As String
 Dim fd As Office.FileDialog

 Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    
 fd.Title = strWindowTitle
 If fd.Show = -1 Then
    DirPicker = fd.SelectedItems(1)
 Else
    DirPicker = vbNullString
 End If
Set fd = Nothing
End Function

Private Sub Export_2_Click()

Dim sPath As String

sPath = DirPicker()
    
    
sPath = Application.CurrentProject.Path
Workbooks.Add.SaveAs FileName:=sPath & "\Aggregated Files.xlsx"

End Sub
 
Upvote 0
Solution
Hi sharonng,

why have a function inside the project if you don´t use the result from it? The dialog delivers the path without the trailing backslash so you would need to add it via
Code:
    DirPicker = fd.SelectedItems(1) & Application.PathSeparator
You could have placed a MsgBox after
Code:
sPath = DirPicker()
MsgBox sPath
or printed the contents to the immediate window to see what the variable holds.

Ciao,
Holger
 
Upvote 0
Hi sharonng,

why have a function inside the project if you don´t use the result from it? The dialog delivers the path without the trailing backslash so you would need to add it via
Code:
    DirPicker = fd.SelectedItems(1) & Application.PathSeparator
You could have placed a MsgBox after
Code:
sPath = DirPicker()
MsgBox sPath
or printed the contents to the immediate window to see what the variable holds.

Ciao,
Holger
Thanks for the update. I was just copying working codes so I don't really have a clue what's going on.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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