+ Reply to Thread
Results 1 to 8 of 8

Macro to Import .txt file into active worksheet, keeping leading zero's

  1. #1
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Macro to Import .txt file into active worksheet, keeping leading zero's

    I'm trying to get together a macro that will allow me to browse a certain folder for a .txt file, and import the selected file while changing columns to text so as to keep the leading zero's in the imported text. The text file will have 12 columns, "|" as the delimiter, and if possible just columns 2 and 8 to be changed to text formatting.

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Call Application.GetOpenFileName for the browse part. With the macro recorder, do File - Open - to run Text Import Wizard on the .txt file, specifying text format for the appropriate columns. Edit the code generated to combine the browse part and copy data from the opened .txt file to your workbook. Hopefully that should be enough to get you started.
    Post responsibly. Search for excelforum.com

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Give this a try

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Alternate method, just change the "00000" to be a number of 0's equal to the the total length including leading 0's that you need. As is, all numbers will become 5 digits with leading 0's, so 123 will become 00123:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  5. #5
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Mike - For some reason your code is giving me a Compile Error: Expected Array message regarding the
    Please Login or Register  to view this content.
    , with the (Lines) highlighted.

    Tigeravatar - The problem with this method is that the numbers may change depending on the .txt file imported, the only way I've actually been able to get them to come in correctly is changing the columns to text during the text import process.

    I attempted to combine my macro recorder code along with the dialogue box to browse for the file, but I'm having trouble having it import the text to the current Workbook and it's formatting, and trouble having the two columns import with text formatting. I'll post the code I'm currently messing with, if anybody has an idea.
    Please Login or Register  to view this content.

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Alright, here's code to change columns B and H to text format and then perform the TextToColumns, let me know if that works for you:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Alternately, this will tell the TextToColumns function to put column 2 and 8 as datatype 2 (Text) during that function:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to Import .txt file into active worksheet, keeping leading zero's

    Thanks very much, it worked great.

+ 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