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