+ Reply to Thread
Results 1 to 6 of 6

Fixed Width Data Importing

Hybrid View

  1. #1
    Registered User
    Join Date
    09-30-2014
    Location
    Columbus, Ohio
    MS-Off Ver
    2010
    Posts
    9

    Fixed Width Data Importing

    I import data into excel regularly and I am looking for help on an annoying feature of excel.

    When importing text and choosing, "Fixed Width", excel tries to decide where to put the "breaks". It is ALWAYS wrong. I then have to remove the "excel suggested breaks" and input my desired "breaks".

    Is there a way to set excel to just let me do the work? I would rather have NO breaks "automatically input", as it takes me longer to remove the suggested breaks than it does to put in the breaks I need. (For example, I just got 28 suggested breaks when I needed only 4!)

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,273

    Re: Fixed Width Data Importing

    You could record a macro to do the import, with the file chosen by you, if your files are consistent enough. Record a macro opening the file, and post it here.

    You could also create your own dialog, but that would be more complicated, so let's start simple.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    09-30-2014
    Location
    Columbus, Ohio
    MS-Off Ver
    2010
    Posts
    9

    Re: Fixed Width Data Importing

    Here is the code that results from clearing the Previous Month's Data, and importing the new data.

    Frankly, I thought of this solution, but couldn't figure out how to "choose the file" while the macro is running.



    Sub Import_FMR_Data()
    '
    ' Import_FMR_Data Macro
    '

    '
    Cells.Select
    Selection.QueryTable.Delete
    Selection.ClearContents
    Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;D:\My HD Documents\EOM Documents\01 Fiscal 2014\01 COGS Reporting\01 Data Reports\2014_09_28 EOM P12\E170_-_CLASS_REPORT.9282014.txt" _
    , Destination:=Range("$A$1"))
    .Name = "E170_-_CLASS_REPORT.9282014_1"
    .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 = xlFixedWidth
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
    .TextFileFixedColumnWidths = Array(50, 20, 20, 20)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,273

    Re: Fixed Width Data Importing

    Here's how to choose the file and create the QueryTable:

    Sub Import_FMR_Data2()
    Dim strName As String
    Dim strTName As String
    
    strName = Application.GetOpenFilename("Text Files (*.txt),*.txt")
    strTName = Replace(Split(strName, "\")(UBound(Split(strName, "\"))), ".txt", "")
    
    On Error Resume Next
    Cells.QueryTable.Delete
    On Error GoTo 0
    Cells.ClearContents
    
    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & strName _
    , Destination:=Range("$A$1"))
    .Name = strTName
    .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 = xlFixedWidth
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
    .TextFileFixedColumnWidths = Array(50, 20, 20, 20)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub

  5. #5
    Registered User
    Join Date
    09-30-2014
    Location
    Columbus, Ohio
    MS-Off Ver
    2010
    Posts
    9

    Re: Fixed Width Data Importing

    THANK YOU!!!

    You just saved me about an hour a month (assuming 30 seconds per download) and a great deal of frustration! (Worth much more than the 30 seconds!)

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,273

    Re: Fixed Width Data Importing

    You can also do multiple files at a time by looping the code - a whole month's worth with one click....

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Importing fixed width .txt files
    By MarmaladeLover in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2014, 08:41 AM
  2. Importing Fixed Width Text Data Into Excel
    By jgsuf in forum Excel General
    Replies: 3
    Last Post: 08-21-2014, 01:48 PM
  3. Problem with importing multiple data files with fixed width format
    By sport_logo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-21-2010, 07:34 AM
  4. [SOLVED] Importing file with fixed width, multi-line records
    By danmcgov in forum Excel General
    Replies: 7
    Last Post: 03-20-2006, 04:10 PM
  5. Importing Fixed Width File Macro
    By Himansu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2005, 01:05 PM

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