Export spreasheet data to txt file
Dear all,
i had a macros to export spreadsheet info into txt.
The spreadsheet is below:
ID info
--- ----
KAS_1 123
KAS_2 456
TAS_1 789
what my script doing is combining data in the excel sheet into one txt file if the first 3 characters in ID column is the same.
The current ouput is:
KAS.txt
--------
1 is 123
2 is 456
(where 1 and 2 is the last character on ID)
TAS.txt
-------
1 is 789
Now , i wish to improve my txt file so that i will have string before and after the contain of the txt file. What should i do in order to get something like below??
Example:
KAS.txt
--------
Hello world 1
1 is 123
2 is 456
Hello world 2
note: the statement is the same for all the txt file.
##### my script #######
Option Explicit
Sub test()
Const startrow As Long = 2
Dim a As Long
Dim id_name As String, id As String, info As String
Dim temp As String
a = startrow
Do While Not ActiveSheet.Cells(a, "A").Value = ""
temp = ActiveSheet.Cells(a, "A").Value
id_name = Left(temp, 3)
id = Right(temp, Len(temp) - 4)
info = ActiveSheet.Cells(a, "B").Value
Call print_output(id, info, id_name)
a = a + 1
Loop
End Sub
Sub print_output(id, info, id_name)
Dim sPath As String
sPath = ThisWorkbook.Path & "/Test/" & id_name & ".txt"
'note use of append: "for output" will
' always overwrite the last item....
Open sPath For Append As #1
Print #1, id & " is " & info
Close #1
End Sub
|