+ Reply to Thread
Results 1 to 14 of 14

Import text file

  1. #1
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Import text file

    Hi

    I've a large text file which I need to import selected information only.

    Below is the sample text file which showing profile for 3 item. Any sample code or ideas would be appreciated.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 09-28-2009 at 12:22 AM. Reason: Added Code tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    re: Import text file

    Hello norhaya,

    Welcome to the Forum!

    To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code
    1. Select all your code using the mouse.
    2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.

    Use the Bulletin Board Code Tags
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] at the end of the last line.


    As a member, You have agreed to follow the forum posting rules. Please take so time to familiarize yourself with the Do's and Don'ts here in the Forum, just click on the link below...

    Forum Rules
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    re: Import text file

    Thank you and noted

  4. #4
    Registered User
    Join Date
    05-20-2009
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    57

    re: Import text file

    Hi,

    you can use the Open statement:
    Please Login or Register  to view this content.
    then read through each line within a loop until EOF. What part(s) of the text file are you specifically looking for?

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    re: Import text file

    Have you read all the rules, if so you should see that your title does not comply
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA code to Import text file

    Quote Originally Posted by bigmack View Post
    Hi,

    you can use the Open statement:
    Please Login or Register  to view this content.
    then read through each line within a loop until EOF. What part(s) of the text file are you specifically looking for?
    I need information as follows and data to be entered as 1 row

    Header
    Item Number, Description, last shipment, last receipt, Average Unit Cost, Stocking Unit, Qty on Hand, additional Information.

    Data
    47, lamp, dd/mmm/yy, dd/mmm/yy, ###,000.00, EA, 50.00, K5501

    I'm self-taught myself in vba, I understand loop code if i work on spreadsheet. however for this I cannot figure out how to capture data in a text file.

    any ideas or example with some explanation will be helpful for me.
    Last edited by norhaya; 09-28-2009 at 04:12 AM. Reason: Wrong Header

  7. #7
    Registered User
    Join Date
    05-20-2009
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    57

    re: Import text file

    Hi Norhaya,

    The header is the easy part. You don't need a macro for that! Here's a basic recipe--create the headers in sheet1. Now, you can begin importing/parsing data from a text file.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    re: Import text file

    Hi BigMack

    Thank you I'll have a look when m back in office. fyi I've a large file and not possible to transfer all into excel 2003. That's why I would like to select relevant information only. I prefer to select relevant which u have given some ideas. My issues here is what the best solution to have the data for item number and description. I tried to go into an option to delete blank rows 1st and then concatenate the two lines. Another problem is, the "item number and description" will be on 1st header in the page of text file, that's why I'm a bit blank here.

    I'll come back n post my code here and let you review and give me feedback
    Last edited by shg; 09-30-2009 at 01:03 PM. Reason: deleted spurious quote

  9. #9
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Import text file

    Quote Originally Posted by norhaya View Post
    Hi BigMack

    Thank you I'll have a look when m back in office. fyi I've a large file and not possible to transfer all into excel 2003. That's why I would like to select relevant information only. I prefer to select relevant which u have given some ideas. My issues here is what the best solution to have the data for item number and description. I tried to go into an option to delete blank rows 1st and then concatenate the two lines. Another problem is, the "item number and description" will be on 1st header in the page of text file, that's why I'm a bit blank here.

    I'll come back n post my code here and let you review and give me feedback
    Hi

    Below is my code, please review and comments, however, I am still not able to call up the part number and description. Any ideas?

    Sub ImportText()

    Dim i As Long, n As Long

    Dim tmpString As String
    Dim tmpAUC As String 'AUC = Average Unit Cost
    Dim Fname As String
    Dim tmpCAT As String 'CAT = Category
    Dim tmpPICK As String ' PICK = Picking Sequence
    Dim tmpUOM As String ' UOM = Unit of Measure
    Dim tmpPN As String ' PN = Part Number
    Dim tmpDesc As String ' Desc = Description
    Dim tmpQOH As Variant ' QOH = Qty on Hand


    Fname = "C:\Import\Accpac.txt"
    Open Fname For Input As #1
    Cells.Clear

    Range("A1:I1").Value = Array("Part No", "Desc", "Date Last Shipment", "Date Last Receipt", "Average Unit Cost", "UOM", "Qty On Hand", "Location", "Remarks")
    tmpPN1 = InStr(tmpString, "") & InStr(tmpString, " ") & InStr(tmpString, "category")
    i = 2
    n = 1
    Do While Not EOF(1)
    Line Input #1, tmpString
    If (InStr(tmpString, "Category")) > 0 Then
    tmpCAT = Right$(tmpString, 12)
    Sheet1.Range("C" & i).Value = tmpCAT

    ElseIf (InStr(tmpString, "Picking")) > 0 Then
    tmpPICK = Right$(tmpString, 12)
    Sheet1.Range("D" & i).Value = tmpPICK
    ElseIf (InStr(tmpString, "Average unit cost")) > 0 Then
    tmpUOM = Right$(tmpString, 16)
    tmpAUC = Mid(tmpString, 44, 14)
    Sheet1.Range("F" & i).Value = Trim(tmpUOM)
    Sheet1.Range("E" & i).Value = tmpAUC

    i = i + 1

    End If

    Loop
    Close #1
    Worksheets("Sheet1").Range("A1:I1").Columns.AutoFit

    End Sub

  10. #10
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Import text file

    Hi Bigmack

    After going thru all the text, if I could join the line before line with "catergory" so it would be 1 string, then I could parse it easily. Can you guide/show me on the code? If this works then I can complete this task.

    Appreciate your assistance.

    Thanks, norhaya
    Last edited by norhaya; 10-02-2009 at 12:37 PM. Reason: to add code tags

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Import text file

    norhaya,

    Please edit your prior post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Registered User
    Join Date
    09-27-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Import text file

    Quote Originally Posted by norhaya View Post
    Hi

    Below is my code, please review and comments, however, I am still not able to call up the part number and description. Any ideas?

    Sub ImportText()

    Dim i As Long, n As Long

    Dim tmpString As String
    Dim tmpAUC As String 'AUC = Average Unit Cost
    Dim Fname As String
    Dim tmpCAT As String 'CAT = Category
    Dim tmpPICK As String ' PICK = Picking Sequence
    Dim tmpUOM As String ' UOM = Unit of Measure
    Dim tmpPN As String ' PN = Part Number
    Dim tmpDesc As String ' Desc = Description
    Dim tmpQOH As Variant ' QOH = Qty on Hand


    Fname = "C:\Import\Accpac.txt"
    Open Fname For Input As #1
    Cells.Clear

    Range("A1:I1").Value = Array("Part No", "Desc", "Date Last Shipment", "Date Last Receipt", "Average Unit Cost", "UOM", "Qty On Hand", "Location", "Remarks")
    tmpPN1 = InStr(tmpString, "") & InStr(tmpString, " ") & InStr(tmpString, "category")
    i = 2
    n = 1
    Do While Not EOF(1)
    Line Input #1, tmpString
    If (InStr(tmpString, "Category")) > 0 Then
    tmpCAT = Right$(tmpString, 12)
    Sheet1.Range("C" & i).Value = tmpCAT

    ElseIf (InStr(tmpString, "Picking")) > 0 Then
    tmpPICK = Right$(tmpString, 12)
    Sheet1.Range("D" & i).Value = tmpPICK
    ElseIf (InStr(tmpString, "Average unit cost")) > 0 Then
    tmpUOM = Right$(tmpString, 16)
    tmpAUC = Mid(tmpString, 44, 14)
    Sheet1.Range("F" & i).Value = Trim(tmpUOM)
    Sheet1.Range("E" & i).Value = tmpAUC

    i = i + 1

    End If

    Loop
    Close #1
    Worksheets("Sheet1").Range("A1:I1").Columns.AutoFit

    End Sub
    Hi Bigmack

    After going thru all the text, if I could join the line before line with "catergory" so it would be 1 string, then I could parse it easily. Can you guide/show me on the code? If this works then I can complete this task.

    Appreciate your assistance.

    Thanks, norhaya

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Import text file

    CODE tags, please, not QUOTE tags, and only around code.

  14. #14
    Registered User
    Join Date
    05-20-2009
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    57

    Lightbulb Re: Import text file

    Norhaya, please follow the rules--simply edit your post and use <code> tags otherwise I cannot even post a hint for you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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