+ Reply to Thread
Results 1 to 4 of 4

Converting a text file to an excel spread sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-16-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    15

    Converting a text file to an excel spread sheet

    This is expanding on a question I had earlier but a little more complicated…..

    I need to import a text file into excel and do the following: (SEE ATTACHED FILE)

    Convert data in rows to columns deleting out information I do not need.
    Data needed is Information on Object #, Name, Coordinates (X,Y,Z), Segment length, Number of Sheets welded, weld type.

    Not all Objects have Coordinates and not all Objects have segment length.
    Attached Files Attached Files
    Last edited by lweng; 09-22-2010 at 08:19 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Converting a text file to an excel spread sheet

    Try this:
    Option Explicit
    
    Sub ImportObjectList()
    'Author:    Jerry Beaucaire
    'Date:      9/16/2010
    'Summary:   Import a text file and extract/format specific data
    Dim fName As String
    Dim LR As Long, FR As Long, NR As Long, Rw As Long
    Dim MyARR As Variant, Vals As Variant
    
    Worksheets.Add
    
    'Select a file
        With Application.FileDialog(msoFileDialogOpen)
            .AllowMultiSelect = False
            .Filters.Add "All Files", "*.*"        'everything
            .Filters.Add "Text Files", "*.txt", 1  'default
            .Show
            If .SelectedItems.Count > 0 Then
                fName = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
        
    Application.ScreenUpdating = False
    'Import the file
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
            Destination:=Range("A1"))
            .Name = "ame--"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(2)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        
    'Create new table
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("B1:I1").Value = [{"OBJECT #","NAME","Coord X","Coord Y","Coord Z","SEG LGTH","WELDS","TYPE"}]
    '    Range("D:F").NumberFormat = "0.00000000000"
        FR = Cells.Find("===", LookIn:=xlValues, LookAt:=xlPart).Row + 1
        MyARR = Application.WorksheetFunction.Transpose(Range("A" & FR & ":A" & LR).Value2)
        NR = 1
        For Rw = LBound(MyARR) To UBound(MyARR)
            If Left(MyARR(Rw), 21) = "Information on object" Then
                NR = NR + 1
                Vals = Split(Trim(MyARR(Rw)), " ")
                Range("B" & NR) = Vals(UBound(Vals))
            ElseIf Left(MyARR(Rw), 4) = "Name" Then
                Vals = Split(Trim(MyARR(Rw)), " ")
                Range("C" & NR) = Vals(UBound(Vals))
            ElseIf Left(MyARR(Rw), 11) = "Coordinates" Then
                Vals = Split(Trim(MyARR(Rw)), " ")
                Range("D" & NR) = Vals(UBound(Vals))
                Vals = Split(Trim(MyARR(Rw + 1)), " ")
                Range("E" & NR) = Vals(UBound(Vals))
                Vals = Split(Trim(MyARR(Rw + 2)), " ")
                Range("F" & NR) = Vals(UBound(Vals))
                Rw = Rw + 2
            ElseIf Left(MyARR(Rw), 14) = "SEGMENT LENGTH" Then
                Range("G" & NR) = Replace(Replace(Trim(MyARR(Rw)), " (string)", ""), "SEGMENT LENGTH = ", "")
            ElseIf Left(MyARR(Rw), 23) = "NUMBER OF SHEETS WELDED" Then
                Vals = Split(Trim(MyARR(Rw)), " ")
                Range("H" & NR) = Vals(UBound(Vals) - 1)
            ElseIf Left(MyARR(Rw), 9) = "WELD_TYPE" Then
                Range("I" & NR) = Replace(Replace(Trim(MyARR(Rw)), " (string)", ""), "WELD_TYPE  = ", "")
        End If
    Next Rw
        
        Columns("A:A").Delete xlShiftToLeft
        Columns("A:H").EntireColumn.AutoFit
        Range("A1:H1").Font.Bold = True
        Range("A2").Select
        ActiveWindow.FreezePanes = True
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-16-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Converting a text file to an excel spread sheet

    Thank you again. I owe you one!

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Converting a text file to an excel spread sheet

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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