1

I have multiple text files in a directory (.hne files) and I want to import the data into Excel but only the data after row 11. The code below imports the data, however it can only do it for one file at a time and my text file has four columns separated by space when it imports to Excel it puts it all in one column.

Can someone please help me clean up this code I would surely appreciate it. Also, I have a lot text files and it would probably exceed the maximum row in one activesheet would it be possible that it automatically puts the rest of the data on another sheet.

Private Sub()
Dim FilePath As String
Dim i, j As Integer
i = 0
j = 1

FilePath = ThisWorkbook.Path & "\201412010030.hne"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(FilePath, 1)
Do Until objFile.AtEndOfStream
    strline = objFile.ReadLine
    'If objFile.AtEndOfStream = True Then
        'MsgBox strLine
    'End If
    i = i + 1
    If i > 11 Then
        Cells(j, 1).Value = strline
        j = j + 1
    End If
Loop
objFile.Close

End Sub

Here is an example of the file im trying to import

   TRIAXYS BUOY DATA REPORT - TAS05642
   VERSION  = 6a.02.08
   TYPE = HNE
   DATE = 2014 Dec 01 01:00
   NUMBER OF POINTS =    2072
   TIME OF FIRST POINT (s)  =   90.63
   SAMPLE INTERVAL (s)  =    0.78
   COLUMN 1 = TIME (s)
   COLUMN 2 = HEAVE (m)
   COLUMN 3 = DSP NORTH (m)
   COLUMN 4 = DSP EAST (m)
   90.63  -0.42   0.53  -0.34
   91.41   0.14   0.72  -0.39
   92.19   0.45   0.61  -0.47
   92.98   0.75   0.26  -0.11
   93.76   1.04  -0.26   0.53
   94.54   0.58  -0.68   0.94
   95.32  -0.13  -0.67   1.00
   96.10  -0.26  -0.47   0.90
   96.88  -0.50  -0.31   0.50
   97.66  -0.94  -0.22  -0.14

This code below works really well, the only thing i need it do is import all the data after row 11.

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("C:\hnefiles")

' 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
       cl.Resize(1, UBound(Items) - LBound(Items) + 1).Value = Items

        ' 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
James Park
  • 11
  • 2
  • you will get a lot farther if you show us what you have tried to 1) import multiple files (hint ... loop through files) and 2) turn data into columns delimited by space ... Once you have researched that, if you have more specific questions around this please edit your question and we can help further. – Scott Holtzman Nov 24 '15 at 17:41
  • First a smal comemnt: Dim i, j As Integer defines i as Variant and only j as Integer! Then I personaly wouldn't use the FSO for readng the file ... would do it line by line with input. And to be honest: I'm not sure if Private Sub() is a valid procedure name ;) – cboden Nov 24 '15 at 17:59
  • Third and most important (as Scott already said) show us what you already have to iterate thru the files. I posted today already 2 times an example that shows how to iterate thru a folder (incl. all level of sub folders) and list the files in them. I do not want to post the same code a third time today :) so have a look at: http://stackoverflow.com/questions/33893133/is-it-possible-to-list-all-the-files-and-folders-in-a-custom-directory-excel-v/33893956#33893956 – cboden Nov 24 '15 at 17:59
  • OK so the files im trying to import, the first 11 rows are the same. – James Park Nov 25 '15 at 13:42

0 Answers0