Import text files contents and name of text file into Excel with a macro

bushidowarrior

Board Regular
Joined
Jun 27, 2011
Messages
84
Office Version
  1. 2019
Platform
  1. Windows
Hello all,

I would like to know if it is possible to import the contents of a text file, and the text filename.

Cats.txt (Contents: grey, blue, black)
Dogs.txt (Contents: pink, red, orange)
Cow.txt (Contents: black, white)

I5P1N1c.png


- If I run the macro again with Updated text files, it will just update the information
- if I add new text files for example "Lion.txt", it will add it to road number 4.
- If the folder is empty, nothing will happen.

Thank you all
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Sorry I forgot to mention that all these text files will be in a folder location which will always be the same.
 
Upvote 0
Hello all, I have made some progress.

I have this code now which can read the file names.

VBA Code:
Sub LoopThroughFiles()
 
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
 
Set oFolder = oFSO.GetFolder("C:\Users\User\Desktop\TEST\")
 
For Each oFile In oFolder.Files
 
    Cells(i + 1, 1) = oFile.Name
 
    i = i + 1
 
Next oFile
 
End Sub

I need to now only make it search for text files and get the contents of the text files in the B column.

Any help would be greatly appreciated.

Thank you
 
Upvote 0
I have progressed a little bit more now, as I have the code only searching for text files.

VBA Code:
Sub findBSAfiles()
Dim objFSO    As Object
Dim objFolder As Object
Dim objFile   As Object
Dim strPath   As String
Dim strName   As String
' Specify the folder...
strPath = "C:\Users\User\Desktop\TEST\"
' Use Microsoft Scripting runtime.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
' Check extension of each file in folder.
For Each objFile In objFolder.Files
If Right(objFile.Name, 4) = ".txt" Then
Cells(i + 1, 1) = objFile.Name
 
    i = i + 1

End If
Next objFile ' Display file names in message box.
MsgBox strName

Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
End Sub

I just need would have guidance to get the contents of the text file please
 
Upvote 0
I have it working now:

VBA Code:
Option Explicit

Sub load()

    ' ADD REFERENCE TO MICROSOFT FILE SYSTEM OBJECT

    Dim objFSO As FileSystemObject
    Dim objFolder As Folder
    Dim objFile As File
    Dim objTextStream As TextStream
    Dim strPath As String
    Dim i As Long

    ' Specify the folder...
    strPath = "C:\Users\User\Desktop\TEST\"

    ' Use Microsoft Scripting runtime.
    Set objFSO = New FileSystemObject
    Set objFolder = objFSO.GetFolder(strPath)

    ' Check extension of each file in folder.
    For Each objFile In objFolder.Files
        If objFSO.GetExtensionName(objFile.Name) = "txt" Then
            Cells(i + 1, 1) = objFile.Name
            Set objTextStream = objFile.OpenAsTextStream(ForReading)
            Cells(i + 1, 2) = objTextStream.ReadAll
            i = i + 1
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,357
Messages
6,124,482
Members
449,165
Latest member
ChipDude83

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