Modify Macro to Select 1 .TXT File at a time, do changes on it (tab delimeter+column to text) to convert to .XLS file+save the file then select other

yferrer

New Member
Joined
Oct 12, 2023
Messages
1
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi hope someone can help me.

I need help to Modify the Macro 1 or the Macro 2
below to Select 1 .TXT File at a time inside a folder, do changes on it (tab delimeter+column to text) on the conversion process (to convert) to .XLS file, then save the file, then select other file and continue. If you have other Macro that works for the same purpose that will be ok too, the importance is not to limit the location of the file to one user, each person needs to be able to run the macro without modifying the need to modify their username or user id on the location of their folder. Example on Macro 2 : "FileToOpen = "C:\Users\ID2040\Desktop\Foldername\Filename.txt" this does not allow other users on other computers to use my Macro.

Macro 1
Sub TXTtoXLS()
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


Macro 2
Sub OpenTXTFileByNameAndFormat()
Dim FileToOpen As Variant

FileToOpen = "C:\Users\E425849\Desktop\Foldername\Filename.txt"
Workbooks.OpenText _
Filename:=FileToOpen, _
DataType:=xlDelimited, _
Tab:=True
'Add Filter

Rows("1:1").Select
Selection.AutoFilter
Cells.Select
Cells.EntireColumn.AutoFit

'Add Color

Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,224,813
Messages
6,181,115
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