Results 1 to 13 of 13

Thread: Appending data of text file to another text file using VBA MS excel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Question Appending data of text file to another text file using VBA MS excel

    Hi,

    Can anyone help me to provide me aVBA script to append data of text file (.txt file) to another text file? My problem is I got around 50 different text file and I want to append the data into single text file.

    My text file contains all the same HEADER. And i want to append the remaining 49 text file into the 1st text fi;e with header already remove since the 1st text file already contain header. Its to tiring for me to do it manually so I open up a question for this.

    Many thanks.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Appending data of text file to another text file using VBA MS excel

    try this, code not tested
    VB Code:
    1. ' assumes all the text files in folder to be processed
    2. myfolder = "c:\test\"
    3. myfile = Dir(myfolder & "*.txt")
    4. If Len(myfile) = 0 Then Exit Sub   ' no txt files found
    5. Open myfolder & "newfile.txt" For Append As 2
    6. Do While Len(myfile) > 0
    7.     Open myfolder & myfile For Input As 1
    8.         filestr = Input(LOF(1), #1)
    9.         Close 1
    10.         filestr = Mid(filestr, InStr(filestr, vbNewLine) + 2) 'remove first line
    11.         Print #2, filestr
    12.         myfile = Dir
    13. Loop
    14. Close 2

    Edit: make sure it does not try to process the output file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Red face Re: Appending data of text file to another text file using VBA MS excel

    Hi Westconn1,
    The code you provide is not working. Maybe need some modification.


    Hi Koolsid,
    I think this one ok but my problem is I have lot's of text file and i want to do it just one select so that all the 49 text files will be appended to the 1st textfile with the headers already removed.

    thanks for all your help

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Appending data of text file to another text file using VBA MS excel

    The code you provide is not working. Maybe need some modification.
    which bit don't work?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Red face Re: Appending data of text file to another text file using VBA MS excel

    on part of:

    Do While Len(myfile) > 0
    Open myfolder & myfile For Input As 1

    It always say file already open.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Appending data of text file to another text file using VBA MS excel

    are you opening a file #1 before that in the code?
    is this the first time through the loop? or is it failing to close 1, though there is only one line between opening and closing, else use freefile

    VB Code:
    1. f1 = FreeFile
    2. Open myfolder & myfile For Input As f1
    3.     filestr = Input(LOF(f1), #f1)
    4.         Close f1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Appending data of text file to another text file using VBA MS excel

    Hi,

    many thanks. Let me try this one first.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Appending data of text file to another text file using VBA MS excel

    Hi

    I think this is what you are looking for...

    http://www.microsoft.com/technet/scr...4/hey1201.mspx

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Appending data of text file to another text file using VBA MS excel

    Koolsid,

    Thanks! its very helpful... But i just have to modify it first coz i want the 2nd and the remaining text file already removed the header before it append. meaning the first lne of text will be removed.

    Thanks again!

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Appending data of text file to another text file using VBA MS excel

    Hi Koolsid,

    I tried the code but im just wondering where the "output.txt" goes? I cannot view the output.

    Can you help me on this?

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Appending data of text file to another text file using VBA MS excel

    1) Where are you saving the output.txt
    2) also are you giving a path in the code?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: Appending data of text file to another text file using VBA MS excel

    Here is the code:

    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objOutputFile = objFSO.CreateTextFile("output.txt")

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='D:\test'} Where " _
    & "ResultClass = CIM_DataFile")

    For Each objFile In FileList
    Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading)
    strText = objTextFile.ReadAll
    objTextFile.Close
    objOutputFile.WriteLine strText
    Next

    objOutputFile.Close

    I put the all my text file in D:\test. Where will I put the output in this code?

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Appending data of text file to another text file using VBA MS excel

    Hi

    This is where you mention the path for the output.txt...

    for ex d:\output.txt

    VB Code:
    1. Set objOutputFile = objFSO.CreateTextFile("output.txt")

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width