Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:53+00:00 2026-06-13T09:03:53+00:00

I found this code on this site but I haven’t been able to adjust

  • 0

I found this code on this site but I haven’t been able to adjust it for my own needs though I think it must be a very quick fix.

The code imports a series of text files to excel. A file is opened and the first line of this file is placed in A1, the second line in A2 and so on. When a new file is opened, the text is placed in the next available cell in column A (all files are read into column A).

I want to make a slight modification. I want the first line of file 1 in A1, the second line in B1 and so on (i.e. all the lines from File 1 are kept in Row 1). Then, the lines in File 2 are placed in Row 2, File 3 in Row 3 etc.

Any help would be greatly appreciated!

    Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' get the directory you want
    Set folder = fso.GetFolder("D:\YourDirectory\")  

    ' set the starting point to write the data to
    Set cl = ActiveSheet.Cells(1, 1)

    ' Loop thru all files in the folder
    For Each file In folder.Files
        ' Open the file
        Set FileText = file.OpenAsTextStream(ForReading)

        ' Read the file one line at a time
        Do While Not FileText.AtEndOfStream
            TextLine = FileText.ReadLine

            ' Parse the line into | delimited pieces
            Items = Split(TextLine, "|")

            ' Put data on one row in active sheet
            For i = 0 To UBound(Items)
                cl.Offset(0, i).Value = Items(i)
            Next

            ' Move to next row
            Set cl = cl.Offset(1, 0)
        Loop

        ' Clean up
        FileText.Close
    Next file

    Set FileText = Nothing
    Set file = Nothing
    Set folder = Nothing
    Set fso = Nothing

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:03:54+00:00Added an answer on June 13, 2026 at 9:03 am

    Yeap. Pretty easy. Just needed to adjust how your columns and rows are being offset and to not have it delimit each line as it’s read.

    See the adjusted code below:

    Sub ReadFilesIntoActiveSheet()
    
    Dim fso As FileSystemObject
    Dim folder As folder, file As file, FileText As TextStream
    Dim TextLine As String, Items() As String
    Dim i As Long, cl As Range
    
    ' Get a FileSystem object
    Set fso = New FileSystemObject
    
    ' get the directory you want
    Set folder = fso.GetFolder("D:\YourDirectory\")
    
    Dim x As Long
    x = 1 'to offset rows for each file
    
    ' Loop thru all files in the folder
    For Each file In folder.Files
    
        ' set the starting point to write the data to
        Set cl = ActiveSheet.Cells(x, 1)
    
        ' Open the file
        Set FileText = file.OpenAsTextStream(ForReading)
    
        Dim i As Long
        i = 0 'to offset columsn for each line
        ' Read the file one line at a time
        Do While Not FileText.AtEndOfStream
    
            TextLine = FileText.ReadLine 'read line
    
            cl.Offset(, i).Value = TextLine 'fill cell
    
            i = i + 1
        Loop
    
        ' Clean up
        FileText.Close
    
        x = x + 1
    
    Next file
    
    Set FileText = Nothing
    Set file = Nothing
    Set folder = Nothing
    Set fso = Nothing
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been asked a dozen times on this site, but I haven't found
I searched through internet and this site as well but haven't found the answer.
I found this code on a hacked site, what does it do exactly? It
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
Checking through my contact form I have found Cross Site Scripting vulnerabilities but haven't
Awesomium easily allows for C++ code to call Javascript methods, but I haven't found
I have searched here, but haven't found any question related to this. I got
I found this code to work on my Drupal site. It outputs the taxonomy
I tried to search this site and all over the internet, but I haven't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.