|
-
Jan 1st, 2007, 03:20 AM
#1
Thread Starter
Hyperactive Member
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.
-
Jan 1st, 2007, 03:55 AM
#2
Re: Appending data of text file to another text file using VBA MS excel
try this, code not tested
VB Code:
' assumes all the text files in folder to be processed
myfolder = "c:\test\"
myfile = Dir(myfolder & "*.txt")
If Len(myfile) = 0 Then Exit Sub ' no txt files found
Open myfolder & "newfile.txt" For Append As 2
Do While Len(myfile) > 0
Open myfolder & myfile For Input As 1
filestr = Input(LOF(1), #1)
Close 1
filestr = Mid(filestr, InStr(filestr, vbNewLine) + 2) 'remove first line
Print #2, filestr
myfile = Dir
Loop
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
-
Jan 2nd, 2007, 12:45 AM
#3
Thread Starter
Hyperactive Member
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
-
Jan 2nd, 2007, 02:28 AM
#4
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
-
Jan 2nd, 2007, 03:05 AM
#5
Thread Starter
Hyperactive Member
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.
-
Jan 2nd, 2007, 03:30 AM
#6
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:
f1 = FreeFile
Open myfolder & myfile For Input As f1
filestr = Input(LOF(f1), #f1)
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
-
Jan 2nd, 2007, 05:20 AM
#7
Thread Starter
Hyperactive Member
Re: Appending data of text file to another text file using VBA MS excel
Hi,
many thanks. Let me try this one first.
-
Jan 2nd, 2007, 01:45 PM
#8
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
-
Jan 2nd, 2007, 11:29 PM
#9
Thread Starter
Hyperactive Member
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!
-
Jan 2nd, 2007, 11:46 PM
#10
Thread Starter
Hyperactive Member
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?
-
Jan 3rd, 2007, 04:22 AM
#11
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
-
Jan 3rd, 2007, 04:48 AM
#12
Thread Starter
Hyperactive Member
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?
-
Jan 3rd, 2007, 12:46 PM
#13
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|