Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 15th, 2006, 01:47 AM
Registered User
 
Join Date: Apr 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old May 18th, 2006, 09:12 AM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Read data from a text file (*.txt) adriant42 Excel VBA 2 June 4th, 2004 06:32 PM
export to .txt file meimy Pro VB.NET 2002/2003 0 May 31st, 2004 02:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.