+ Reply to Thread
Results 1 to 16 of 16

Text To Excel

Hybrid View

rocky2011 Text To Excel 01-04-2011, 01:16 AM
pike Re: Text To Excel 01-05-2011, 04:58 AM
pike Re: Text To Excel 01-05-2011, 06:42 AM
rocky2011 Re: Text To Excel 01-06-2011, 03:48 AM
pike Re: Text To Excel 01-06-2011, 08:48 PM
rocky2011 Re: Text To Excel 01-07-2011, 02:43 AM
pike Re: Text To Excel 01-07-2011, 03:01 AM
rocky2011 Re: Text To Excel 01-07-2011, 03:18 AM
pike Re: Text To Excel 01-07-2011, 03:57 AM
rocky2011 Re: Text To Excel 01-07-2011, 04:22 AM
pike Re: Text To Excel 01-07-2011, 05:20 AM
rocky2011 Re: Text To Excel 01-07-2011, 06:59 AM
pike Re: Text To Excel 01-07-2011, 07:24 AM
rocky2011 Re: Text To Excel 01-11-2011, 04:32 AM
foxguy Re: Text To Excel 01-12-2011, 11:28 AM
rocky2011 [Solved] Re: Text To Excel 02-03-2011, 01:05 AM
  1. #1
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Smile Text To Excel

    hi Everyone

    I basically have a large text file and I want to convert into Excel exactly as I have attached both text & excel files here.
    Can anyone help me how can I convert/import this 'text' file into Excel
    P.S.: Please note that text file data is too large this is just sample so I can't do 'tab' settings.
    any other shorter way if anyone know would be highly appriciated

    Thanks
    Rocky
    Attached Files Attached Files
    Last edited by pike; 02-03-2011 at 02:46 AM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi Rocky
    can you check the text file as the data has different layouts
    Is the data grouped over four or five lines?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi Rocky
    with out knowing the possible data layouts something like this
    Sub ReadTxt()
        Dim tmpCode, lastObj As String
        Open "C:\ Your File Path \rocky text.txt" For Input As #1
        Do While Not EOF(1)
            Codes = ReadLines
            xRow = xRow + 1
            If Codes(0) <> "End_Of Line" Then
                xCol = 1
                For xcount = 1 To 5
                    For Each xValue In Split(Codes(xcount), " ")
                                  If xValue <> "" Then
                    Cells(10 + xRow, xCol).Value = xValue
                   xCol = xCol + 1
                   End If  
      xCol = xCol + 1
                   
                   Next xValue
                Next xcount
            Else
                GoTo exitLoop
            End If
        Loop
    exitLoop:
        Close #1
    End Sub
    Function ReadLines() As Variant
        On Error GoTo endfile
        Dim codeStr, valStr As String
        Line Input #1, LineOne
        Line Input #1, LineTwo
        Line Input #1, LineThree
        Line Input #1, LineFour
        Line Input #1, LineFive
        Line Input #1, LineSix
        ReadLines = Array(LineOne, LineTwo, LineThree, LineFour, LineFive, LineSix)
        Exit Function
    endfile:
        ReadLines = Array("End_Of Line", "End_Of Line")
    End Function
    Last edited by pike; 01-05-2011 at 07:01 AM. Reason: add Cells(10 + xRow, xCol).Value = xValue

  4. #4
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Text To Excel

    Thnks very much Pike for your reply & the suggestion.
    And yes you are right. text file data are grouped over four or five lines.
    In text file you can see that data is starting with "MD-DD-XXXXXX" & ending with "1 TO 2 YEARS," and total no. of data for one entry/raw is 26. i.e. in excel the data should be imported from A1 to Z1, A2 to Z2, A3 to Z3............and so on.
    I hope you can understand & for your complete knowledge I am attaching herewith a sample of Text file that contains 147 Data. In text file you will see as I have written above the
    Entry no. 1 starts with MD-DD-030000, XC-DD-25001,SN1OOOO25001,.................1 TO 2 YEARS (Total 26 columns)
    Entry no. 2 starts with MD-DD-030001, XC-DD-25002,SN1OOOO25002,.................1 TO 2 YEARS (Total 26 columns)
    ........................................................................
    .......................................................................
    Entry no. 147 starts with MD-DD-030146, XC-DD-25147, SN1OOOO25147,.................1 TO 2 YEARS (Total 26 columns)

    Now I want to convert this 147 Entries into Excel & for your ready reference I am attaching sample of excel file(1st 3 entries) as what exact data I want to be imported all 147 entries into excel, I have also given the Titles to all 26 columns maybe that can help out in importing.

    I am awaiting your reply.

    Thnks & Rgds
    Rocky
    Attached Files Attached Files

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi rocky
    This will just add the text file to the worksheet per 4 or five grouping but there are to many variables to work out what goes where
    How do you create the text file? If you could add the delimiters to the text file it would be easier
    Option Explicit
    Sub ReadString()
        Dim sLine As String
        Dim sFName As String
        Dim iFNumber As Integer
        Dim lRow As Long
        Dim LColumn As Long
        Dim vValues
        Dim iCount As Integer
        sFName = "C:\"Your Path"\147 Entry.txt"
        iFNumber = FreeFile
        Open sFName For Input As #iFNumber
        lRow = 6
        LColumn = 0
        Do
            Line Input #iFNumber, sLine
            vValues = Split(sLine, " ")
            With Sheets("Sheet1")
                For iCount = LBound(vValues) To UBound(vValues)
                    If vValues(iCount) = "," Then
                        lRow = lRow + 1
                        LColumn = 0
                    Else
                        LColumn = LColumn + 1
                        .Cells(lRow, LColumn) = vValues(iCount)
                    End If
                Next iCount
            End With
        Loop Until EOF(iFNumber)
        Close #iFNumber
    End Sub

  6. #6
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Text To Excel

    Thanks very much Pike for your reply.
    I have tried your last code but sorry it didn't worked!
    Well the original file is PDF and I have converted into text. The data of text file is too large so adding delimiters or applying tab will take too long time!
    But one common thing is that there are exactly 26 columns in each entry( you can see the excel file i have attached earlier)

    Thanks
    Rocky

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi Rocky
    conversion from PDF is a problem as there is no real pattern to follow; Its near impossible to allow for all the variations in the layout.
    The PDF is created from a word document?

  8. #8
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Text To Excel

    Well Pike I have some softwares that can easily turn any pdf into word exactly the data as it is. and not only pdf , like jpg files can also be converted to word easily.
    And i really don't know that pdf is created from word, cuz I hv rcvd. pdf.
    Tell me if you want to see the pdf, i will attach it.
    thnks
    Rocky

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi Rocky
    veiwing the pdf won't help.. it just the way the data is layed-out.. to many variables to capture them all fro the table..
    Can you control how the data is inputed?

  10. #10
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Text To Excel

    Pike , well I really don't know how the data is inputed into pdf, but I think maybe through some software or programme.
    Rgds
    Rocky

  11. #11
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi rocky2011
    change the file path name to the txt files and this will take out another variable
    Option Explicit
    Sub ReadStringLines()
        Dim sLine As String
        Dim sFName As String
        Dim iFNumber As Integer
        Dim lRow As Long
        Dim LColumn As Long
        Dim vValues
        Dim iCount As Integer
        Dim mMarker As Boolean
        sFName = "C:\????????????????\147 Entry.txt"' Add Correct File Path
        iFNumber = FreeFile
        Open sFName For Input As #iFNumber
        lRow = 2
        LColumn = 0
        Do
            Line Input #iFNumber, sLine
            vValues = Split(sLine, " ")
            With Sheets("Sheet2")
                For iCount = LBound(vValues) To UBound(vValues)
                   If iCount = 0 And Left(vValues(iCount), 6) = "assign" And LColumn = 0 Then
                     mMarker = True
                   ElseIf iCount = 0 And vValues(iCount) = "Page" And LColumn = 0 Then
                     mMarker = False
                   End If
                    If vValues(iCount) = "," Then
                        lRow = lRow + 1
                        LColumn = 0
                    ElseIf mMarker Then
                        LColumn = LColumn + 1
                        .Cells(lRow, LColumn) = vValues(iCount)
                    End If
                Next iCount
    
            End With
        Loop Until EOF(iFNumber)
        Close #iFNumber
    End Sub

  12. #12
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Text To Excel

    Hi Pike,
    Sorry but last code didn't work at all. I rcvd. a blank file in excel.
    And I am applying correct file path and that is c:\147.txt
    For your ready reference I am enclosing both 147 txt & 147 excel files
    so that you can workout more on it.

    thnks very much
    Rocky
    Attached Files Attached Files

  13. #13
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Text To Excel

    Hi rocky2011,
    147.txt has another different layout with added line breaks
    since the txt file was made from a PDF I'm afraid it is not practical to convert it to the table you require

  14. #14
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Text To Excel

    Hi Pike,
    I have attached a new text of 147 entries in a different format. Where you will see that each entry is seperated by a comma, can you please take a look on file and give me your feedback?
    Thnks very much
    Rocky
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Text To Excel

    Hi Rocky;

    I don't work with text files, but since you asked me to look at it I'll try.
    This code DEPENDS on a "," being at the end of each line and a SPACE, or CR or LF to separate the text for each column.

    I tried it on "new 147.txt" and it worked fine, but I don't see any reason why it would not work on any of the formats you have uploaded.

    Sub ReadTextFile()
        Dim sLine As String
        Dim sLongLine As String
        Dim sFName As String
        Dim iFNumber As Integer
        Dim lRow As Long
        Dim iColumn As Integer
        Dim vValues
        Dim iCount As Integer
    
        sFName = "C:\????????????????\147 Entry.txt"        ' Add Correct File Path
        iFNumber = FreeFile
        Open sFName For Input As #iFNumber
        lRow = 2
        iColumn = 0
        Do
            sLongLine = ""
            Do While InStr(sLongLine, ",") = 0
                Line Input #iFNumber, sLine
                sLongLine = sLongLine & " " & sLine
                'Debug.Print sLine
                'Debug.Print sLongLine
                If EOF(iFNumber) Then
                    Exit Do
                End If
            Loop
            vValues = Split(sLongLine, " ")
            If IsArray(vValues) Then
                With Sheets("Sheet2")
                    For iCount = LBound(vValues) To UBound(vValues)
                        If vValues(iCount) = "," Then
                            lRow = lRow + 1
                            iColumn = 0
                        ElseIf Trim(vValues(iCount)) = "" Then
                            'skip this value
                        Else
                            iColumn = iColumn + 1
                            .Cells(lRow, iColumn) = vValues(iCount)
                        End If
                    Next iCount
                End With
            End If        'IsArray(vValues)
        Loop Until EOF(iFNumber)
        Close #iFNumber
    End Sub
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  16. #16
    Registered User
    Join Date
    01-04-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    9

    [Solved] Re: Text To Excel

    Thnx very much Pike, its worked correctly. For larger data also this will work although I have to make some changes in text file. Thanks again for your help.
    Rocky

+ 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