+ Reply to Thread
Results 1 to 18 of 18

Import text file into excel and split data in specific sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Import text file into excel and split data in specific sheets

    Hello,
    I attached my .txt data file named "data". I also attach the excel file, to help you to understand what i really need.
    I need a VBA solution for import the informations in excel.
    The challenge is to split this data into multiple sheets, by "Node Name".

    for example:
    in sheet 1, will be imported data from node name 17GHEB, the sheet name will become 17GHEB.
    so on

    best regards, i hope somebody can help me
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Import text file into excel and split data in specific sheets

    Sub Import_Node_Text()
    Dim fs, fil, cur_line, s_name, row_count
    Set fs = CreateObject("Scripting.filesystemobject")
    Set fil = fs.opentextfile("E:\data.txt")
    Do
        On Error GoTo end_file
        row_count = 1
        Sheets.Add After:=Sheets(Sheets.Count)
        Range("A" & row_count) = cur_line
        row_count = 2
        Do Until Strings.Left(cur_line, 9) = "Node Name"
            cur_line = fil.readline
            Range("A" & row_count) = cur_line
            row_count = row_count + 1
        Loop
        s_name = Strings.Left(cur_line, InStr(1, cur_line, "Board ID") - 10)
        s_name = Replace(s_name, "Node Name    : ", "")
        ActiveSheet.Name = Replace(s_name, " ", "")
        Do
            cur_line = fil.readline
            If Strings.Left(cur_line, 5) = "Daily" Then Exit Do
            Range("A" & row_count + 1) = cur_line
            row_count = row_count + 1
        Loop
    Loop
    Exit Sub
    end_file:
    End Sub

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    Thanks yudlugar! that code works really fine. but that informations are copied into one column.
    I need to get that data in separate columns. In fact, i need to get only day, volume and energy. can be done?
    Anyway, i appreciate your effort, you are really good!
    Best regards,
    Ionut

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Import text file into excel and split data in specific sheets

    Try this
    Sub test()
        Dim fn As String, x, n As Long, t As Long, e
        fn = ThisWorkbook.Path & "\data.txt"  '<- file path
        x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbCrLf)
        With CreateObject("VBScript.RegExp")
            .Global = True
            For Each e In x
                .Pattern = "^Node Name *: *(\S+)"
                If .test(e) Then
                    t = t + 1: n = 1
                    If t > Sheets.Count Then
                        Sheets.Add(after:=Sheets(Sheets.Count)).Name = _
                        .Execute(e)(0).submatches(0)
                    Else
                        Sheets(t).Name = .Execute(e)(0).submatches(0)
                        Sheets(t).Cells.Clear
                    End If
                    Sheets(t).Cells(n, 1).Resize(, 4).Value = _
                    [{"day","Flow hrs.","Energy kWh","Volume m3"}]
                End If
                If n > 0 Then
                    .Pattern = "^ *(\d+) *(\d+\.\d{2}).* +(\d+\.\d{2}) *(\d+\.\d{2}) *$"
                    If .test(e) Then
                        n = n + 1
                        Sheets(t).Cells(n, 1).Resize(, 4).Value = _
                        Split(.Replace(e, "$1-$2-$3-$4"), "-")
                    End If
                End If
            Next
        End With
    End Sub

  5. #5
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    yudlugar, that works amazing!! thanks! you are the best! have a nice day!
    Last edited by chirilaionut; 05-30-2013 at 06:57 AM.

  6. #6
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    wow!
    Thank you both! you are really amazing!
    both answers works just fine!!
    thank you again!
    Last edited by chirilaionut; 05-30-2013 at 07:01 AM.

  7. #7
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Import text file into excel and split data in specific sheets

    I came up with this:
    Option Explicit
    
    Sub Import_Node_Text()
    Dim fs, fil, cur_line, split_line, s_name, row_count
    Set fs = CreateObject("Scripting.filesystemobject")
    Set fil = fs.opentextfile("E:\data.txt")
    Do
        On Error GoTo end_file
        row_count = 1
        Sheets.Add After:=Sheets(Sheets.Count)
        Range("A" & row_count) = cur_line
        row_count = 2
        Do Until Strings.Left(cur_line, 9) = "Node Name"
            cur_line = fil.readline
            Range("A" & row_count) = cur_line
            row_count = row_count + 1
        Loop
        s_name = Strings.Left(cur_line, InStr(1, cur_line, "Board ID") - 10)
        s_name = Replace(s_name, "Node Name    : ", "")
        ActiveSheet.Name = Replace(s_name, " ", "")
        Do
            cur_line = fil.readline
            If Strings.InStr(1, cur_line, "Flags") Then
                Range("A" & row_count) = "Day"
                Range("B" & row_count) = "Energy"
                Range("C" & row_count) = "Volume"
                row_count = row_count + 1
                cur_line = fil.readline
                cur_line = fil.readline
                Do
                    cur_line = fil.readline
                    If Strings.Left(cur_line, 1) = "-" Then Exit Do
                    Range("A" & row_count) = Replace(Strings.Left(cur_line, 9), " ", "")
                    Range("C" & row_count) = Replace(Strings.Right(cur_line, 15), " ", "")
                    Range("B" & row_count) = Replace(Replace(Strings.Right(cur_line, 35), " ", ""), Range("C" & row_count), "")
                    row_count = row_count + 1
                Loop
            End If
            If Strings.Left(cur_line, 5) = "Daily" Then Exit Do
            Range("A" & row_count + 1) = cur_line
            row_count = row_count + 1
        Loop
    Loop
    Exit Sub
    end_file:
    End Sub

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Import text file into excel and split data in specific sheets

    Did you change the filepath?

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Import text file into excel and split data in specific sheets

    Place both Excel and text file in the same folder and try again.

    It is working here.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    a stupid question: if i want to import other column (for example: "pressure" from that .txt file, what should i change into the jindon's code?
    i looked at the code 1 hour today and i still feel stupid...
    thanks again, you are doing a great job!

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Import text file into excel and split data in specific sheets

    Try this
    Sub test()
        Dim fn As String, x, n As Long, t As Long, e
        fn = ThisWorkbook.Path & "\data.txt"  '<- file path
        x = Split(CreateObject("Scripting.FileSystemObject") _
            .OpenTextFile(fn).ReadAll, vbCrLf)
        With CreateObject("VBScript.RegExp")
            .Global = True
            For Each e In x
                .Pattern = "^Node Name *: *(\S+)"
                If .test(e) Then
                    t = t + 1: n = 1
                    If t > Sheets.Count Then
                        Sheets.Add(after:=Sheets(Sheets.Count)).Name = _
                        .Execute(e)(0).submatches(0)
                    Else
                        Sheets(t).Name = .Execute(e)(0).submatches(0)
                        Sheets(t).Cells.Clear
                    End If
                    Sheets(t).Cells(n, 1).Resize(, 5).Value = _
                    [{"day","Flow hrs.","Pressure","Energy kWh","Volume m3"}]
                End If
                If n > 0 Then
                    .Pattern = "^ *(\d+) *(\d+\.\d{2}).\D+ +(\d+\.\d{2}).*(\d+\.\d{2}) *(\d+\.\d{2}) *$"
                    If .test(e) Then
                        n = n + 1
                        Sheets(t).Cells(n, 1).Resize(, 5).Value = _
                        Split(.Replace(e, "$1-$2-$3-$4-$5"), "-")
                    End If
                End If
            Next
        End With
    End Sub
    Last edited by jindon; 05-31-2013 at 08:50 AM.

  12. #12
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    thanks man! you rulllzzzzzzz!

  13. #13
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    jindon, help me please! from "energy" are imported only last number before dot and decimales. (ex from 1234.44 are imported 4.44)
    Last edited by chirilaionut; 06-03-2013 at 03:04 AM.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Import text file into excel and split data in specific sheets

    Can you just add one space after 3 rd asterisk?
                If n > 0 Then
                    .Pattern = "^ *(\d+) *(\d+\.\d{2}).\D+ +(\d+\.\d{2}).* (\d+\.\d{2}) *(\d+\.\d{2}) *$"

  15. #15
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    Thx, u r big in japan!
    have a nice day!

  16. #16
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    jindon, could you explain the logic of this code?
    i understand it about 90%, but something it's not very clear to me. for instance, i try to extend that code for 6 columns:

    Sheets(t).Cells(n, 1).Resize(, 6).Value = _
    [{"day","Flow hrs.","presure","diff","Energy kWh","Volume m3"}]
    End If
    If n > 0 Then
    .Pattern = "^ *(\d+) *(\d+\.\d{2}).\D+ *(\d+\.\d{2})*(\d+\.\d{2}).\D+ +(\d+\.\d{2}).* (\d+\.\d{2}) *(\d+\.\d{2})*(\d+\.\d{2}) *$"

    but, it appear a blank on "pressure" column. what i did wrong?
    it seems that i am not smart enough...

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Import text file into excel and split data in specific sheets

                If n > 0 Then
                    .Pattern = "^ *(\d+) *(\d+\.\d{2}).\D+ +(\d+\.\d{2}) *(\d+\.\d{2}).* (\d+\.\d{2}) *(\d+\.\d{2}) *$"
                    If .test(e) Then
                        n = n + 1
                        Sheets(t).Cells(n, 1).Resize(, 6).Value = _
                        Split(.Replace(e, "$1-$2-$3-$4-$5-$6"), "-")
                    End If
                End If

  18. #18
    Registered User
    Join Date
    06-12-2012
    Location
    bucharest
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Import text file into excel and split data in specific sheets

    finally i get it!! i understand the meaning of code! you are great man, i really apreciate your work! thanks very much again, best regards!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1