VBA code save as excel instead of txt

spill-the-beans

Board Regular
Joined
Feb 7, 2013
Messages
52
Hello,

I'm trying to make some code that will let me select the active workbook (which is a txt file) and save the first worksheet page (the only page with data in that workbook) as an excel file, then close the file.

Would it be possible to code that so that no file names are specified? Seeing as I want the excel file the same name as whatever the worksheet is called. That way, I can just run the code on all open workbooks instead of having to click through them all. If anyone has any idea that could help, I'd really appreciate your advice.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
well, Spill, I assume you are looking at a specific directory for the file name, all you should need to do is scan the folder for files ending in text, capture the file name, then in VBA open the file and do a SaveAs with the file name and extension, for example:
Code:
Excel.Application.Workbooks.Open ("c:\" & filename.txt & ")
Excel.Application.ActiveSheet.Name = filename
excel.Activeworkbook.SaveAs "c:\" & filename & ".xlsx"
Excel.Application.Quit

If you have multiple files in the same directory, you can wrap it in a For Each loop with the code above and it would open each text file and save as xlsx.
 
Upvote 0
That sounds like a perfect solution. Sorry to be cheeky, but could you tell me how I should write the For Each loop in the code? I've not written them myself before.
 
Upvote 0
I've just tried the code in the mean time, and realised that it would open the text files. Is there a way that I can just save the active workbooks with the loop? I would already have them open in excel. All I need to do is save the open text files as excel files, not all the text files in the directory. This is what I've got to work at the moment.

Code:
Sub SaveTxtFiles()


Dim Filename As String


Application.DisplayAlerts = False


Filename = ActiveSheet.Name
ActiveWorkbook.SaveAs "S:\Documents\DataFiles\Scenes\Adults\" & Filename & ".xlsx"


ActiveWorkbook.Close


Application.DisplayAlerts = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,619
Members
449,240
Latest member
lynnfromHGT

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