LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default write to CSV

On 19 Oct, 10:56, keiji kounoike <"kounoike A | T ma.Pikara.ne.jp"
wrote:
One way, but not so fast.
If your workbook's name is Book1.xls, this macro would create a file
named Book1.csv in the same folder with Book1.xls

Sub Worbook2Csv()
Dim Csvname As String, Pdir As String
Dim TmpWB As Workbook, AcWB As Workbook
Dim Fname() As String
Dim SelSh As Sheets, Wsh As Worksheet
Dim filenum
Dim i As Long

Pdir = ActiveWorkbook.path
Csvname = ActiveWorkbook.Name
Csvname = Left(Csvname, InStr(Csvname, ".") - 1)
ChDir Pdir
Set AcWB = ActiveWorkbook
Set SelSh = AcWB.Worksheets

SelSh.Copy
Set TmpWB = ActiveWorkbook

Application.DisplayAlerts = False
Application.ScreenUpdating = False

ReDim Fname(TmpWB.Worksheets.Count - 1)

For Each Wsh In TmpWB.Worksheets
* * *Wsh.Select
* * *TmpWB.SaveAs Filename:=Csvname & "Tmp" & CStr(i), _
* * * * *FileFormat:=xlCSV, CreateBackup:=False
* * *Fname(i) = TmpWB.FullName
* * *i = i + 1
Next
TmpWB.Close

Open Fname(0) For Append As #1
For i = 1 To UBound(Fname)
* * *filenum = FreeFile
* * *Open Fname(i) For Input As #filenum
* * *Do While Not EOF(filenum)
* * * * *Line Input #filenum, tmp
* * * * *Print #1, tmp
* * *Loop
* * *Close #filenum
* * *Kill Fname(i)
Next
Close #1

Name Fname(0) As Replace(Fname(0), "Tmp0", "")

Keiji

cyew wrote:
Hi


I am new to VBA.


I have data on a number of sheets and I would like to write VBA codes
to loop through each sheet and write out the data in one CSV file.


eg


Sheet1
1 *one
2 *two
3 *three


Sheet2
4 *four
5 *five
6 *sixe


I would like my output CSV file to be:
1,one
2,two
3,three
4,four
5,five
6,sive


Thanks
Chen




Thanks All for your help.

Chen

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write VBA March Excel Programming 2 January 16th 09 08:11 PM
re write [email protected][_2_] Excel Programming 2 April 6th 06 12:36 PM
is it possible to execute write to the fields in another .xsl form a macro in another .xsl? e.g. some way to load another .xsl into an .xsl macro and write to its data? Daniel Excel Worksheet Functions 1 June 23rd 05 11:38 PM
Better way to write this Steph[_3_] Excel Programming 2 May 11th 05 11:33 PM
How to I write a UDF? Dr. Juzzy Excel Discussion (Misc queries) 1 January 30th 05 02:17 PM


All times are GMT +1. The time now is 11:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"