Hi. I'm creating a tool to save some repetitive work for coworkers but can't figure out how to name the new excel workbook with the same name as the file the VBA opened.
What should happen:
Users browses to the target file (like a text file), it'll copy the data to an Excel workbook then browse again to find the targeted folder then save the Excel with the same name as the text file.
i.e. it opens a file called MyNotePadFile.txt, dumps the data therein into an excel the saves it as MyNotePadFile.xlsx
My code does this all but I can't figure how to duplicate the filename. Right now, it saves the file as YourFileName:
I tried to use strFileToOpen in the place of "YourFileName" but that didn't work (Runtime error 1004 method SaveAs of object _Workbook failed). I feel like I'll slap myself once the solution is shown but I can't figure it out. Thanks for the guidance!
What should happen:
Users browses to the target file (like a text file), it'll copy the data to an Excel workbook then browse again to find the targeted folder then save the Excel with the same name as the text file.
i.e. it opens a file called MyNotePadFile.txt, dumps the data therein into an excel the saves it as MyNotePadFile.xlsx
My code does this all but I can't figure how to duplicate the filename. Right now, it saves the file as YourFileName:
VBA Code:
Sub ReconFiles()
Dim strFileToOpen As Variant, fldr As FileDialog, selectedFolder As String, SheetName As String
strFileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to open")
If strFileToOpen = False Then
MsgBox "No file selected.", vbExclamation, "Try again!"
Exit Sub
Else
Workbooks.Open FileName:=strFileToOpen
End If
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.Show
selectedFolder = .SelectedItems(1)
End With
ActiveWorkbook.SaveAs Filename:=selectedFolder & "\" & "YourFileName" & ".xlsx"
I tried to use strFileToOpen in the place of "YourFileName" but that didn't work (Runtime error 1004 method SaveAs of object _Workbook failed). I feel like I'll slap myself once the solution is shown but I can't figure it out. Thanks for the guidance!