VBA to convert large volume of text files (*.txt) to (*.xlsm) files

Dojorgen

Board Regular
Joined
Mar 1, 2018
Messages
59
First timer, high excel skills but low VBA skills.

I have 1000+ .txt files that i need to convert to .xlsm files on a daily basis. I have been searching for options but have not had much luck.

I have a R: drive that is holding all of these files i need a macro that can open and save as .xlsm and close.

Any help would be appreciative.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Thanks i did see that i was not savvy enough to figure out how to modify it to pull from my R: drive
 
Upvote 0
I have amended it to rename the file as an .xlsm file.

The code will prompt you for the Directory. Give it a try.

Code:
Option Explicit


Sub foo()
 Dim MyFolder As String
 Dim myfile As String
 Dim folderName As String


 With Application.FileDialog(msoFileDialogFolderPicker)
 .AllowMultiSelect = False
 If .Show = -1 Then


 folderName = .SelectedItems(1)
 End If
 End With


 myfile = Dir(folderName & "\*.txt")


 Do While myfile <> ""
 Workbooks.OpenText Filename:=folderName & "\" & myfile
'save as excel file
 ActiveWorkbook.SaveAs Filename:=folderName & "\" & Replace(myfile, ".txt", ".xlsm")
'use below 3 lines if you want to close the workbook right after saving, so you dont have a lots of workbooks opened
 Application.DisplayAlerts = False
 ActiveWorkbook.Close
 Application.DisplayAlerts = True
 myfile = Dir
 Loop
End Sub
 
Upvote 0
Understood, thank you go it working right

Follow up question, is there a way to modify the saveas command to allow me to save myfile under different folders for example:
if file name has 2W1234 in the name it would get saved under 2018/RTP then if file name has 2W5678 in the name it would get saved under 2018/CFM, n.......?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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