Consulting

Results 1 to 20 of 20

Thread: Solved: Solved:How to get several .txt file data in a workbook?

  1. #1
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location

    Solved: Solved:How to get several .txt file data in a workbook?

    Hi,everyone

    Usually,I can open a .txt file and get its data in a workbook.Now,I have two or more .txt files in a folder or in a directory.How can I get these .txt file data in the same workbook? Can I make these .txt files as a Collection Object?

    Thanks for any assistance!
    Last edited by fanjy; 07-29-2006 at 01:10 AM. Reason: Solved:How to get several .txt file data in a workbook?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This might help with your task.....might need tweeking to suit your needs:
    [VBA]
    Option Explicit
    Sub ImportTextUsingXlDialogOpen()

    '\ use this approach to prompt user for text wizard values

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    ActiveSheet.UsedRange.Clear
    Range("A1").Select

    '\ display open file dialog and copy to new (temporary workbook)
    If Application.Dialogs(xlDialogOpen).Show("*.txt") Then
    ActiveSheet.UsedRange.Select '\ select imported text in temporary workbook
    Selection.Copy '\ copy to clipboard
    ActiveWorkbook.Close '\ close temporary workbook
    ActiveSheet.Paste '\ paste text into your workbook
    End If
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location

    Smile

    Steve,Thank you very much.
    But I don't want to I open then Text Guide dialog.
    use VBA,only one time ,total these .txt files in a workbook.

    The following is Excel file and .txt files.

  4. #4
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Hi
    try
    note: if you have more than 65536 line to input, need modification
    [vba]
    Sub Sample()
    Dim n As Long, a(), ff As Integer, txt As String, myDir As String, x
    Dim myF As String, i As Long
    myDir = "C:\test\" ' change here to suite
    myF = Dir(myDir & "*.txt")
    Do While myF <> ""
    ff = FreeFile
    Open myF For Input As #ff
    Do While Not EOF(ff)
    Line Input #ff, txt
    x = Split(txt)
    Redim Preserve a(1 To n)
    a(n) = x
    Loop
    Close #ff
    myF = Dir()
    Loop
    With ThisWorkbook.Sheet1.Range("a1")
    For i = 1 To UBound(a)
    .Offset(i).Resize(,UBound(a(i))) = a(i)
    Next
    End With
    End Sub[/vba]

  5. #5
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location
    Hi,jindon
    Thank you for your Code!Sorry,I am a newer.When I run the Macro,Excel display "File Not Found!".Why?see:

  6. #6
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    fanjy,

    Sorry, I'm not able to see your file at the moment.

    You need to change myDir to your actual directory.

  7. #7
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Or place the text files in the same folder that workbook is in
    [vba]
    Sub Sample()
    Dim n As Long, a(), ff As Integer, txt As String, myDir As String, x
    Dim myF As String, i As Long
    myDir = ThisWorkbook.Path & Application.PathSeparator
    myF = Dir(myDir & "*.txt")
    Do While myF <> ""
    ff = FreeFile
    Open myF For Input As #ff
    Do While Not EOF(ff)
    Line Input #ff, txt
    x = Split(txt)
    Redim Preserve a(1 To n)
    a(n) = x
    Loop
    Close #ff
    myF = Dir()
    Loop
    With ThisWorkbook.Sheet1.Range("a1")
    For i = 1 To UBound(a)
    .Offset(i).Resize(,UBound(a(i))) = a(i)
    Next
    End With
    End Sub[/vba]

  8. #8
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location

    Smile

    jindon:
    I have tried,but failed.
    Statement:ThisWorkbook.Sheet1.Range("a1") ,Should change:ThisWorkbook.Worksheets("Sheet1").Range("a1")
    When run The Statement:
    Open myF For Input As #ff
    display:
    "File Not Find!"
    You can see my file in #5.

  9. #9
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    OK
    Can you change like

    Open myDir & myF For Input As #ff

  10. #10
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location
    again
    the statement:
    ReDim Preserve a(1 To n)
    brings up a "subscript" error.

  11. #11
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Good sign!
    [vba]
    Sub Sample()
    Dim n As Long, a(), ff As Integer, txt As String, myDir As String, x
    Dim myF As String, i As Long
    myDir = ThisWorkbook.Path & Application.PathSeparator
    myF = Dir(myDir & "*.txt")
    Do While myF <> ""
    ff = FreeFile
    Open myDir & myF For Input As #ff
    Do While Not EOF(ff)
    Line Input #ff, txt
    x = Split(txt, "|")
    n = n + 1
    Redim Preserve a(1 To n)
    a(n) = x
    Loop
    Close #ff
    myF = Dir()
    Loop
    With ThisWorkbook.Sheets("Sheet1").Range("a1")
    For i = 1 To UBound(a)
    .Offset(i - 1).Resize(,UBound(a(i))+1) = a(i)
    Next
    End With
    End Sub[/vba]
    Last edited by jindon; 07-29-2006 at 12:56 AM.

  12. #12
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location
    Also see:
    .Offset(i).Resize(,UBound(a(i))) = a(i)
    display:"error 1004"

  13. #13
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Yep

    I should consider for blank line..

    .Offset(i).Resize(,UBound(a(i))+1) = a(i)

    Code above has been modified already...

  14. #14
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location
    Ok,succeed.Thank you.
    But these data is filled in column A.
    Can you fill these data in different column?For example,
    SaleData|Sales amount|ID|POSID|BankerID|
    column A B C D E
    and ,not display "|" sign.
    see:

  15. #15
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    OK, so the file is delimited by |, I thought it would be a space....

    Can you just change the line of

    x = Split(txt)

    to

    x = Split(txt, "|")

    will change the code later....

  16. #16
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location
    jindon,
    Perfectly!Thanks.
    But in my sheet, First row is blank.Can you make data begin from the first row?

  17. #17
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Ok

    Change

    .Offset(i).Resize(,....

    To

    .Offset(i-1).Resize....

    code has been modified....

  18. #18
    VBAX Regular
    Joined
    May 2006
    Posts
    27
    Location
    jindon,Thank you very much.
    The question has solved.
    vbaexpress is a good place for my study.

  19. #19
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Nice job Jindon, thanks for following up on this.
    fanjy, if you have your answer please mark your thread solved using thread tools at the top of the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  20. #20
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    fanjy,
    Glad I could help

    Steve
    Thanks and I've just modified the code to coop with more than 65536 lines..
    [vba]
    Sub Sample()
    Dim n As Long, a(), ff As Integer, txt As String, myDir As String, x
    Dim myF As String, i As Long, t As Integer
    t = 1
    myDir = ThisWorkbook.Path & Application.PathSeparator
    myF = Dir(myDir & "*.txt")
    Do While myF <> ""
    ff = FreeFile
    Open myDir & myF For Input As #ff
    Do While Not EOF(ff)
    Line Input #ff, txt
    x = Split(txt, "|")
    n = n + 1
    Redim Preserve a(1 To n)
    a(n) = x
    If n = 65536 Then
    With ThisWorkbook.Sheets(t).Range("a1")
    For i = 1 To UBound(a)
    .Offset(i- 1).Resize(,UBound(a(i))+1) = a(i)
    Next
    End With
    n = 0 : Erase a : t = t + 1
    Loop
    Close #ff
    myF = Dir()
    Loop
    If n > 0 Then
    With ThisWorkbook.Sheets(t).Range("a1")
    For i = 1 To UBound(a)
    .Offset(i - 1).Resize(,UBound(a(i))+1) = a(i)
    Next
    End With
    End If
    End Sub[/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •