+ Reply to Thread
Results 1 to 2 of 2

VBA code to convert text file to Excel 2010

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2011
    Location
    Boston, US
    MS-Off Ver
    Excel 2013
    Posts
    9

    VBA code to convert text file to Excel 2010

    Hi Everyone,

    I am trying to convert a txt file to excel as below. I did a lot of search via google, but no VBA code fit to my case. Can anyone offer some help on my problem, any input is greatly appreciated!
    Attached is my sample .txt file.

    Supplier Name Supplier Num Taxpayer ID Site Name Address1 City State Zip Code
    Attached Files Attached Files

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: VBA code to convert text file to Excel 2010

    Do you want to choose you TXT file manually? A window can be opened asking to select the file.
    Are those fields the only ones you want to retreive?
    Try this macro:
    Public Sub Extract_Data()
    Dim Open_File As String, Str As String, R_ow As Long
    Open_File = Application.GetOpenFilename()
    Open Open_File For Input As #1
    On Error GoTo Err_or 'Just in case of an error so the TXT file is closed properly
    R_ow = Cells(Rows.Count, 1).End(xlUp).Row
    '---Search data through the file
    Do While Not EOF(1)
       Line Input #1, Str
       If InStr(1, Str, "Supplier Name:", vbTextCompare) <> 0 Then
          Cells(R_ow, 1) = Trim(Mid(Str, 16, 42)) 'Supplier name
          Cells(R_ow, 3) = Trim(Mid(Str, 72, 30)) 'Taxpayer ID
          Line Input #1, Str
          If InStr(1, Str, "Supplier num:", vbTextCompare) <> 0 Then
            Cells(R_ow, 2) = Trim(Mid(Str, 16, 30)) 'Supplier Number
          End If
       End If
       If InStr(1, Str, "Site Name", vbTextCompare) <> 0 Then
          Line Input #1, Str 'This one reads the dashes
          Line Input #1, Str 'This one reads the site name and address1
          Cells(R_ow, 4) = Trim(Mid(Str, 9, 15)) 'Site name
          Cells(R_ow, 5) = Trim(Mid(Str, 25, 35)) 'Address1
          Line Input #1, Str 'This one reads the City, State and Zip code
          With CreateObject("VBScript.RegExp")
             .IgnoreCase = True
             .MultiLine = True
             .Global = True
             'Pattern is most important as it returns us the different value we are looking for
             .Pattern = "(\w{1,20}).(\w{2}).(\w{5,7})"
             Str = Trim(Str)
             If .test(Str) Then
                Cells(R_ow, 6) = .Replace(Str, "$1") 'City
                Cells(R_ow, 7) = .Replace(Str, "$2") 'State
                Cells(R_ow, 8) = .Replace(Str, "$3") 'Zip code
             End If
          End With
       End If
       'Here, we look for the word "Page:" that marks the separation of 2 suppliers in the report
       'we increase the row by one to write on the next row
       If InStr(1, Str, "Page:", vbTextCompare) <> 0 Then R_ow = R_ow + 1
    Loop
    Err_or:
    Close #1
    End Sub
    Please note that it does not erase data before iimporting the TXT file. It appends data to the existing one.
    Last edited by p24leclerc; 03-20-2014 at 05:31 PM.
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

+ 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. Excel 2010-convert text to value
    By Ramsee in forum Excel General
    Replies: 19
    Last Post: 01-15-2014, 07:10 AM
  2. Excel 2010 file with VBA save and send code works-but file can't be found.
    By carlton.clay in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2013, 09:46 AM
  3. Convert Text to Numeric Excel 2010 Pivot Table
    By zcheema in forum Excel General
    Replies: 1
    Last Post: 05-17-2012, 04:58 PM
  4. Convert Excel to formatted text file text file
    By rbpd5015 in forum Excel General
    Replies: 2
    Last Post: 01-14-2011, 10:27 AM
  5. VB code to auto save file as excel 2010
    By bryanc2k in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-14-2010, 01:44 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