Hello cozandeffect,
This macro will read all text files in a specified folder. The first line is assumed to be the title of the article. This will placed in "A1" on "Sheet1". You can change the starting cell and sheet name to what you are using. These appear in bold type. The remaining lines are placed in column "B".
Copy Text Files to Worksheet
Sub ReadTextFiles()
Dim FileName As String
Dim FilePath As String
Dim LineCnt As Long
Dim R As Long
Dim Rng As Range
Dim Text As String
Dim Wks As Worksheet
Set Wks = Worksheets("Sheet1")
Set Rng = Wks.Range("A1")
FilePath = "C:\Documents and Settings\Admin.ADMINS\My Documents\Excel Forum Folders\cozandeffect"
FileName = Dir(FilePath & "\*.txt")
Do While FileName <> ""
LineCnt = 0
N = FreeFile
Open FilePath & "\" & FileName For Input As #N
Do While Not EOF(N)
Line Input #N, Text
LineCnt = LineCnt + 1
If Text = "" Then Text = " "
If LineCnt = 1 Then
Rng.Offset(R + LineCnt - 1, 0).Value = Text
Else
Rng.Offset(R + LineCnt - 1, 1).Value = Text
End If
Loop
Close #N
R = R + LineCnt
FileName = Dir()
Loop
End Sub
Adding the Macro- Copy the macro above pressing the keys CTRL+C
- Open your workbook
- Press the keys ALT+F11 to open the Visual Basic Editor
- Press the keys ALT+I to activate the Insert menu
- Press M to insert a Standard Module
- Paste the code by pressing the keys CTRL+V
- Make any custom changes to the macro if needed at this time.
- Save the Macro by pressing the keys CTRL+S
- Press the keys ALT+Q to exit the Editor, and return to Excel.
To Run the Macro...
To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Bookmarks