Convert .txt to .xls

SamCha

New Member
Joined
Nov 23, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I am trying to write a code that will help me convert an .xls extension file to the correct .xls File Format 56. The file that needs to be converted will be user selected. Currently, the files are in txt format but saved with .xls extension. So I am converting the file into text and then trying to save the file as .xls. The file name of the final excel file will be the same as the converted text file.

Please can you help in understanding how to proceed in saving the converted text file with the name of the text file as xls file format 56.

Rich (BB code):
Sub folderaccess()
Dim fd As Office.FileDialog
Dim strFile As String
Dim BaseName As String
Dim File_Path As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Filters.Add "Excel Files", "*.xls?", 1
.Title = "Choose an Excel 97-2003 file"
.AllowMultiSelect = False
.InitialFileName = "C:\VBA Folder"
If .Show = True Then
strFile = .SelectedItems(1)
End If
ThisWorkbook.Names("File_Path1").RefersToRange.Value = strFile
End With
Base_Name = Mid(strFile, InStrRev(strFile, "\") + 1, InStrRev(strFile, ".") - InStrRev(strFile, "\") - 1)
ThisWorkbook.Names("Base_Name").RefersToRange.Value = Base_Name
Name strFile As (Base_Name & ".txt")
End Sub

Thanks.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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