+ Reply to Thread
Results 1 to 3 of 3

Pulling data from a text file, splitting by fixed column width

Hybrid View

ferynd Pulling data from a text... 05-14-2013, 03:45 PM
JBeaucaire Re: Pulling data from a text... 05-14-2013, 05:59 PM
JBeaucaire Re: Pulling data from a text... 05-15-2013, 08:21 PM
  1. #1
    Registered User
    Join Date
    10-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1

    Pulling data from a text file, splitting by fixed column width

    I need to pull a large table of data from a notepad .txt file. Presently, I'm trying to pull it into a new work book, apply the fixed column split, and then copy and paste it over to my work book. Unfortunately this is not working and I've tried so much code now my own macro is just looking convoluted and painful. Any help would be highly appreciated.

    I was hoping that the text to column conversion with the array setup would split this appropriately but it's not splitting the data at all. I'm not sure why. If anyone knows, am I just formatting the numbers wrong? I thought the first number was the character start positions and the second was the number of characters to include.

    Sub Pull_FlashReport_Fiscal()
    
    Dim master As Workbook
    Dim slave As Workbook
    
    Set master = ActiveWorkbook
    
    Application.DisplayAlerts = False
    
    'clear old data
    
    Sheet4.Cells.ClearContents
    
    'pull data from notepad
    Workbooks.OpenText Filename:="C:\Documents and Settings\User\Desktop\Excel Project\Sales Sheet\FLASH_REPORT_FOR_GASA_-_FISCAL.4292013.txt", _
        Origin:=-535, _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, _
        Tab:=False, _
        Semicolon:=False, _
        Comma:=False, _
        Space:=False, _
        Other:=False
    
    Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(1, 5), Array(5, 33), Array(38, 12), Array(50, 10), Array(60, 15), Array(75, 13), Array(88, 12), Array(100, 11), Array(111, 8), Array(119, 7), Array(126, 7)), _
        TrailingMinusNumbers:=True
    
    Set slave = ActiveWorkbook
    
    ActiveSheet.Cells.Select
    Selection.Copy
    
    master.Activate
    Sheet4.Activate
    Sheet4.Range("A1").Activate
    Sheet4.Paste
    
    slave.Close
    
    Application.DisplayAlerts = True
    
    End Sub
    Last edited by ferynd; 05-14-2013 at 04:28 PM.

  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: Pulling data from a text file, splitting by fixed column width

    You're close. The first parameter is the character position to the LEFT of the position you want next. The second parameter is data type. 1 is text.

    So, to get the first character set of text use (0, 1). If the first set was 5 characters, then the NEXT group will indicate that with (5, 1) which means start with the character after the 5th and use data type 1. Etc.


    So maybe:
    Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(32, 1), Array(37, 1), Array(49, 1), Array(59, 1), Array(74, 1), Array(87, 1), Array(99, 1), Array(110, 1), Array(118, 1), Array(125, 1)), TrailingMinusNumbers:=True
    Last edited by JBeaucaire; 05-14-2013 at 06:01 PM.
    _________________
    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
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Pulling data from a text file, splitting by fixed column width

    Thanks for the feedback.

    I've marked this thread as SOLVED for you.
    Next time, select Thread Tools from the links above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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