|
|
 |
| Excel VBA Discuss using VBA for Excel programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Excel VBA section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

May 15th, 2006, 02:47 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

May 18th, 2006, 10:12 AM
|
|
Registered User
|
|
Join Date: May 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
You can modify your code like
a = startrow
Call print_output("Hello World", 1, id_name)
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
Call print_output("Hello World", "2", id_name)
Regards,
Yogesh.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |