VBA- how to save as a filename

Js Smith

Board Regular
Joined
Jul 24, 2020
Messages
51
Office Version
  1. 2010
Platform
  1. Windows
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:

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!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Replace the ActiveWorkbook.SaveAs line with:
VBA Code:
    Dim p1 As Long, p2 As Long
    p1 = InStrRev(strFileToOpen, "\")
    p2 = InStrRev(strFileToOpen, ".")
    ActiveWorkbook.SaveAs Filename:=selectedFolder & "\" & Mid(strFileToOpen, p1 + 1, p2 - p1 - 1) & ".xlsx"
 
Upvote 0
Solution
Thank you, John!
Worked like a charm. Appreciating the help and the education. :giggle:
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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