vba code for text to excel

rxsp

New Member
Joined
Feb 4, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am having a hard time trying to create a vba code to perform a specific task.
I want the user to be able to select the folder where all the text files are located.
Then, have the VBA code to convert multiple text files into excel. So each text file will have its own excel spreadsheet (I do not want to combine the text files). And then close the text files and excel spreadsheets in that same folder that was initially selected by the user.
The folder name changes every month and so does the text file titles.
I was able to use the Macro to convert a text file into an excel spreadsheet, but I don't know how to assign the variables and be able to do that for each text file (looping).
Macro to convert text file into excel spreadsheet:

app.Workbooks.OpenText Filename:=myFile, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _

xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _

Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _

Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _

Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1)), _

TrailingMinusNumbers:=True
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi, a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
      Const E = ".txt"
        Dim P$, Obj As Object, F$
    Do
         If P > "" Then Beep
        Set Obj = CreateObject("Shell.Application").BrowseForFolder(0, vbLf & "Select the text files folder :", 1, "")
         If Obj Is Nothing Then Exit Sub Else P = Obj.Self.Path & "\": Set Obj = Nothing
               F = Dir$(P & "*" & E)
    Loop Until F > ""
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    Do
        Workbooks.Open P & F, , , 1
        ActiveWorkbook.SaveAs P & Replace(F, E, ""), 51
        ActiveWorkbook.Close
               F = Dir$
    Loop Until F = ""
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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