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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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