+ Reply to Thread
Results 1 to 18 of 18

Browse to file (DAT) & Import fixed column widths

  1. #1
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Browse to file (DAT) & Import fixed column widths

    I have scoured the web and tried out many potential codes, but nothing works for me. I found the code below over at OzGrid, and I made changes to the array to suit my needs, and a few other changes (file type was txt, I changed to DAT, as this will be used by someone else). I have spent 2 days on trying to do this myself (Friday and today). I am guesing that I am close, just not quite there. Any help is greatly appreciated!

    My data is as follows:
    Start: 0, 1, 10, 13, 23, 29, 37, 46, 49, 58, 59, 61, 63
    Length: 1, 9, 3, 10, 6, 8, 9, 3, 9, 1, 2, 2, 3

    The data file ends at 66 characters.

    I would like all columns set as General, except the following:
    2, 3, 4 - Text
    6 - Date (MDY)


    Please Login or Register  to view this content.
    Attached Images Attached Images
    ~*~ Sherry ~*~
    Poinciana, FL

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    I'd recommend uploading sample .DAT file (or .txt).

    For fixed width import, you need different syntax than standard field array using Text Import Wizard.
    You'll need to set .TextFileColumnDataTypes = Array(#, #, #....) as well as .TextFileFixedColumnWidths = Array(#, #...) and other properties.

    You can try recording process of manually importing the file to get the base syntax.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Re: Browse to file (DAT) & Import fixed column widths

    CK76,

    Unfortunately I only have the one (confidential) DAT file. And Excel will not me to use the Text Import Wizard, it forces me to do a query. so I cannot go through the steps to record as you suggest and also see the data to provide s sample file.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    Can't you desensitize the info? From your description, you just changed file extension from .txt to .DAT. So you have access to the structure of the file and what's in it.
    Trim it down to about 10 lines, and replace sensitive info with generic one. While maintaining the structure. That should give us enough to work with.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Browse to file (DAT) & Import fixed column widths

    If you use Debug menu's Compile before a run, you would see a problem with FName. That should be vPath.

    Not all DAT files are TXT files. If more problems, try changing name to .txt. That will not help if it is not a text file.

  6. #6
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Re: Browse to file (DAT) & Import fixed column widths

    CK76---

    Actually I ha not changed the file from DAT to TXT, so I just finally did that to provide a sample. Attached is a sample file consisting of approximately 10 rows of data. What will become my template will have headers in row 1, so when imported I would like this data to start on row 2. However I would like the current top and bottom rows of the file to be deleted. They are info that the source uses that have no meaning for us. Please remember that I have this set for a DAT file. I want the user to only select the correct file; you can select "all files" to allow selection of the TXT file.


    Kenneth---

    I changed FName to vPath as you suggested, however the same error showed.
    Attached Files Attached Files

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Browse to file (DAT) & Import fixed column widths

    I will look into making the standard method more tailored to your needs. For now, try recording a macro to see if it does what you want suggested in post #2.

    You can do the math to make the fixed list needed or use this. After a run, the result is in the Immediate window. Or, copy the comment less the single quote for use in the recording. I think that the array string needs a tweak.

    Please Login or Register  to view this content.
    I guess you know how to record a macro?

    To do the manual import, in a new sheet on the ribbon: Data > Get Data > From File > From Text/CSV > select your file > for delimited, choose Fixed Width > below it paste, 0,1,10,13,23,29,37,46,49,58,59,61,63,66 > Transform Data > Load.
    Last edited by Kenneth Hobson; 10-01-2019 at 11:23 AM.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    Here's basic code.

    I've got to go into meeting and don't have time to adjust for formatting. But it should give you good starting point.
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Re: Browse to file (DAT) & Import fixed column widths

    Kenneth,

    I added the Legacy Text Import back in. For this project, the power query causes issues with the data. Although I try to select the format for a few columns, the text is no problem. The issue is with my one date column. Power Query deletes the leading 0, and then gives an error, so the data in that field will not convert to a date. When I use the "legacy" text import wizard (which is easier for the user), that does not happen, and this date field remains a date. Yes I know how to record a macro. The recording below is when I used my DAT file and use of the text import wizard.

    Please Login or Register  to view this content.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    Since date string is just numeric string that does not conform to standard formatting convention. You'll need bit of special operation.

    For Numbers that should be stored as text. You can simply set cell's number format to "@".

    Ex:
    Please Login or Register  to view this content.
    Note: You can adjust starting row of data storage by adding last row (first empty row) # to j.

  11. #11
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Re: Browse to file (DAT) & Import fixed column widths

    CK76,

    That works well except 2 things:

    1) column G data is somehow being formatted as a date also, it should be general. Only column F is a date (and only B, C, D are text).
    2) I need the ability to browse for a file; hard coding the file name (changes every week) and path (multiple users) is not an option

    Now I also have a question. I am attempting to understand the syntax of your arrays in this script. I see the 2nd # relates to the length, but I cannot figure out the first #, as it does not match what I provided. Is this a better way to do it? Oh, 1 other question. In your date format for column F, I think I may need to display the date as: MM/DD/YYYY, we are waiting for confirmation. I see the date in the "case 5" area, but I am unfamiliar with that format. How would I change it to reflect the format I mentioned?

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    Column G in my environment does come in as General. Not Date. If you need to just set NumberFormat to general for the target cell when Case 6.

    As for using MM/DD/YYYY format for Case 5 (column F).
    Format() function is just converting number to date string. When this value is returned to sheet/cell, it's automatically interpreted as Date value. You can apply any date formatting you want to the target cell.
    Ex:
    Please Login or Register  to view this content.
    Sample result:
    0.JPG

    First # is starting position. In VBA character position starts from 1 not zero. So I just added 1 to your positions

    I prefer this method over legacy wizard, as it gives me more control over what transformation is needed for each step.

    I'm out of time for now, but to select multiple .DAT files, you can use FileDialog() method. Will see if I have time later.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    Here you go. Had bit of time. This will give you file dialog to select files and pre-filtered for *.DAT files.

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Re: Browse to file (DAT) & Import fixed column widths

    CK76----

    That works great! But I was trying to make you proud of me! LOL I was making adjustments to the code for the "browse" option I wanted. I also added the headers, I could not seem to add it to this same script so I created another for that purpose, and called it. It does work, but I have 1 issue I am hopping you might be willing to assist on tomorrow. This macro will be in a new template for the user, and to execute they will just push a button (Easy Peasy!). Now the sheet where the macros/buttons are: MAIN (codename: Sheet2). This data needs to be imported into DAT data tab (codename: Sheet4). Now because the user MAY change the sheet names, I want the macro to reflect the codename (Sheet4) for where this data is to import to. I could not figure out where to add it. I tried a few options, and they simply added on the active sheet. Thanks in advance, you have been awesome in helping me resolve my challenge.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by Webbers; 10-01-2019 at 04:58 PM.

  15. #15
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Browse to file (DAT) & Import fixed column widths

    Please Login or Register  to view this content.

  16. #16
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Browse to file (DAT) & Import fixed column widths

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor Webbers's Avatar
    Join Date
    09-06-2005
    Location
    Poinciana, FL
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    337

    Re: Browse to file (DAT) & Import fixed column widths

    Kenneth---

    I only have one thing to say to you.... WoooHooo!!!!! Thanks so much for finalizing the changes I requested. I greatly appreciate you doing these final changes.


    CK76----

    Kenneth was able to update your code with my final change request. Thanks so much for everything. Your time and help are greatly appreciated!!!!!

  18. #18
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Browse to file (DAT) & Import fixed column widths

    You are welcome and thanks for the rep

    If your question is satisfactorily answered. Please mark the thread as solved, using Thread tool found at top of your initial post.

+ 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. Browse to htm file and import
    By webwyzard in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2018, 08:22 AM
  2. Using an Array to set Column Widths when Importing a Fixed Width Text File
    By ericbartha in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2016, 03:41 PM
  3. Replies: 2
    Last Post: 08-25-2015, 09:57 AM
  4. [SOLVED] Macro to import .csv from browse for file location dialog box.
    By gutterball in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2014, 08:30 AM
  5. Browse and import .csv file into new tab
    By alf40 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2013, 11:22 PM
  6. [SOLVED] Format column Widths - Free and Fixed
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2010, 01:09 PM
  7. [SOLVED] Varying Column Widths for Text Import
    By Rytrilius in forum Excel General
    Replies: 3
    Last Post: 11-28-2005, 10:00 PM

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