VBA Excel - Import Text File Names into A Column

shana

New Member
Joined
Jun 23, 2013
Messages
22
Hi,

greetings to all.

I was hoping some one may be able to share some coding expertise on how I may be able to adapt a macro found on the forum.

Apologies if there is a complete solution already , I have not been able to adapt it to solve my problem.

I am using this vba macro to import text files. This works perfectly as it imports the whole text file with all the original spaces and lines into a single cell - which I need.

I am trying to work out how to import the file names on a separate column.

Original - Import Text Files

http://www.mrexcel.com/forum/excel-questions/462499-import-whole-text-file-into-single-cell.html

Code:
Dim filename As String, nextrow As Long
  Dim sPath As String
    Dim iRow As Long
    Dim strString  As String
    
    Dim fso As FileSystemObject
    Dim xFile As File
    Dim xFolder As Folder
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set xFolder = fso.GetFolder("C:\Users\Desktop\Import\")
    
    iRow = 2 ' Row to start inserting data
    
    For Each xFile In xFolder.Files
        
        If InStr(1, xFile.Name, ".txt") <> 0 Then
        
            Dim lFile As Long
            Dim szLine As String
            
            lFile = FreeFile()
            
            Open xFile.Path For Input As lFile
            
            strString = ""
            While Not EOF(lFile)
            
                Line Input #lFile, szLine
            
                ' Concatenete lines from text file
                strString = strString & szLine & vbCrLf
                
            Wend
            
            ' Add to cell
            Cells(iRow, 1).Value = strString
            
            iRow = iRow + 1
            
            ' Close the file
            Close lFile
            
         Cells(nextrow, "A").Value = xFile.Name     '  Add the filename

            Application.ScreenUpdating = True
                        
        End If
        
    Next ' End of LOOP
    
    MsgBox "Completed!"

I am simply trying to import the file names as well at the same time , I have tried adapting the code
to import the file name but am not any closer to a solution.:confused:

Cells(nextrow, "A").Value = xFile.Name ' Add the filename into column A or column of choice

I am stuck here and not sure how to fix this.

I would be grateful if some one would kindly advise on how I can import the file names at the same time. I would prefer that this macro was adapted so I can simply import in one go, or I don't mind if there is a similar macro that imports the complete text file with its original formatting into a single cell and then the file-names in a separate column.

I have seen macros that can be used to import text file names - but those are separate and standalone. I am bit worried that I may import files and then import the wrong file names with a separate macro.

Thank you so much for your help in advance :)

S
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi

Your almost there.
If you want the filename in column A, then you will need to move the text import to column B

Code:
' Add to cell
            Cells(iRow, [COLOR=#ff0000][/COLOR][SIZE=2][/SIZE][SIZE=3][/SIZE][SIZE=3][COLOR=#ff0000]2[/COLOR][/SIZE]).Value = strString

            [FONT=Verdana][/FONT][SIZE=1][/SIZE][SIZE=2][/SIZE][SIZE=3][/SIZE][SIZE=2][FONT=Verdana]Cells(irow, [COLOR=#ff0000]1[/COLOR]).Value = xFile.Name
[/FONT][/SIZE]
            iRow = iRow + 1
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
 
Upvote 0
ahh thanks Roger for your help

smashing! Did the job perfect.

Here's the final with the new code added.

Code:
Option Explicit
Sub ImportCompleteTextFileAndName()


'Imports Each Text Files into a single cell in Column B
'Imports the File Name into Column A

    
    Dim sPath As String
    Dim iRow As Long
    Dim strString  As String
    
    Dim fso As FileSystemObject
    Dim xFile As File
    Dim xFolder As Folder
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set xFolder = fso.GetFolder("C:\Users\Desktop\Import\")
    
    iRow = 2         ' Row to start inserting data
    
    For Each xFile In xFolder.Files
        
        If InStr(1, xFile.Name, ".txt") <> 0 Then
        
            Dim lFile As Long
            Dim szLine As String
            
            lFile = FreeFile()
            
            Open xFile.Path For Input As lFile
            
            strString = ""
            While Not EOF(lFile)
            
                Line Input #lFile, szLine
            
                ' Concatenete lines from text file
                strString = strString & szLine & vbCrLf
                
            Wend
            
          
            
            ' Add to cell
            Cells(iRow, 2).Value = strString
            
            
            '========= Import the File Names

            Cells(iRow, 1).Value = xFile.Name        ' Imports the file name into Column A

            iRow = iRow + 1
            
        
            ' Close the file
            Close lFile
            
            Application.ScreenUpdating = True
                        
        End If
        
    Next ' End of LOOP
    
    MsgBox "Completed!"
    
    
    'Adapted From Original
    'http://www.mrexcel.com/forum/excel-questions/462499-import-whole-text-file-into-single-cell.html
    
  
    
End Sub

Thank you again - Have a great day :)

S

Please Mark Problem Solved !
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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