VBA open all text files in excel in a folder and save them as excel file

linoutandabout

New Member
Joined
Feb 27, 2010
Messages
10
I have some daily text files in a folder (so about 30 of them each month), which in the end of month, I need to open them up in excel, format them so that I can use the information for my analysis.

I would like to create a macro, to quickly open them all up at once and save them each individually in .xls or .xlsm format.

I am new to VBA and after some research online, I was able to have the files open with the following code. but now I don't know how to proceed further to save them one by one with the same name but in .xls or .xlsm format. :confused:

Would really appreciate your help on this!

Thanks!

Sub Opentxtfiles()
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
myfile = Dir
Loop


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Just change it like this:
Code:
Private Sub CommandButton1_Click()
 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", ".xls")
'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
Hello
I was looking a code like this on internet, but i have to do some modifications.

Every month y have different folders that contains csv and txt files.
The txt files have to be separated with the text to columns option and the csv files data comes separated with '|'.

This is what i diid with the code but I get an error saying that i cannot save the file with the same name as the original one.

Dim MyFolder As String
Dim myfile As String
Dim folderName As String

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

folderName = .SelectedItems
End If
End With

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

Do While myfile <> ""
Workbooks.OpenText Filename:=folderName & "\" & myfile, Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
Array(4, 1), Array(6, 1), Array(12, 1), Array(15, 1), Array(21, 1), Array(24, 1)), TrailingMinusNumbers:=True
Call FormatoArchivosTxt


'save as excel file
ActiveWorkbook.SaveAs Filename:=folderName & "\" & Replace(myfile, ".txt", ".xlsx")
'use below 3 lines if you want to close the workbook right after saving, so you dont have a lots of workbooks opened


' ActiveWorkbook.Close
' myfile = Dir
Loop
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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