Vba - Save .txt file in Excel format, while keeping the orginal name?

MyriamSL

New Member
Joined
Jul 12, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to find a way to add a ''save'' function at the end of my macro to save my file in Excel format (originally a .txt file), while keeping the same name the .txt had. I was able to either do one or the other (Excel file that didn't retain the original name, or text file with the right, original name), but not both at the same time.

I'm sure it's really not that complicated, but I'm a vba newbie.

Thanks!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this:
VBA Code:
Sub SaveFile()
  Dim xName As String
  
  Application.DisplayAlerts = False
  xName = Replace(ActiveWorkbook.Name, ".txt", ".xlsx")
  ActiveWorkbook.SaveAs Filename:=xName, FileFormat:=xlOpenXMLWorkbook
  ActiveWorkbook.Close False
  Application.DisplayAlerts = True
End Sub

If this is related to your other question, then the complete code is:
VBA Code:
Sub Open_Txt_and_save()
  Dim xName As String
  
  With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select txt file"
    .Filters.Add "Txt Files", "*.txt"
    .AllowMultiSelect = False
    .InitialFileName = "C:\trabajo"
    If .Show Then
      Workbooks.OpenText Filename:=.SelectedItems.Item(1), _
        Origin:=xlWindows, _
        StartRow:=1, _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, _
        Tab:=False, _
        Semicolon:=False, _
        Comma:=True, _
        Space:=False, _
        Other:=False
    End If
  End With
  
  Application.DisplayAlerts = False
  xName = Replace(ActiveWorkbook.Name, ".txt", ".xlsx")
  ActiveWorkbook.SaveAs Filename:=xName, FileFormat:=xlOpenXMLWorkbook
  ActiveWorkbook.Close False
  Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

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