Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 31st, 2006, 03:07 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How To Export a specific Excel Sheet as a csv file

hey all:

I was wondering if there was a way in EXCEL VBE to export a specific Excel Worksheet (not the entire workbook) as a .csv file. Thanks
Reply With Quote
  #2 (permalink)  
Old August 1st, 2006, 10:44 AM
Registered User
 
Join Date: Jul 2006
Location: Bernareggio, , Italy.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a way to do it:

ActiveWorkbook.SaveAs Filename:="Filename.csv", _
        FileFormat:=xlCSV

Ciao

Mario
Reply With Quote
  #3 (permalink)  
Old August 1st, 2006, 01:55 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

unfortunately, that seems to save the entire workbook. is there a way i can just save one worksheet (like Sheet1)? Thanks
Reply With Quote
  #4 (permalink)  
Old August 1st, 2006, 02:41 PM
Authorized User
 
Join Date: Jul 2006
Location: , , Poland.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This module exports current region from worksheet to .csv file. Mayby it help You.

Sub ExportRange()
Dim FirstCol As Integer
Dim LastCol As Integer
Dim C As Integer
Dim FirstRow
Dim LastRow
Dim R
Dim data
Dim ExpRng As Range
Range("A1").Select
    Set ExpRng = ActiveCell.CurrentRegion
    FirstCol = ExpRng.Columns(1).Column
    LastCol = FirstCol + ExpRng.Columns.Count - 1
    FirstRow = ExpRng.Rows(1).Row
    LastRow = FirstRow + ExpRng.Rows.Count - 1

    Open ThisWorkbook.Path & "\textfile.csv" For Output As #1 ' csv file
    '''''''Open ThisWorkbook.Path & "\textfile.txt" For Output As #1 '''''or txt file
        For R = FirstRow To LastRow
            For C = FirstCol To LastCol
                data = ExpRng.Cells(R, C).Value
                If data = "" Then data = ""
                If IsNumeric(data) Then data = Val(data)
                If C <> LastCol Then
                    Write #1, data;
                Else
                    Write #1, data
                End If
            Next C
        Next R
    Close #1
End Sub

Reply With Quote
  #5 (permalink)  
Old August 1st, 2006, 03:45 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you. that does help :)
Reply With Quote
  #6 (permalink)  
Old August 1st, 2006, 04:04 PM
Authorized User
 
Join Date: Jul 2006
Location: , , Poland.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default


I have the next solution (I think that it is better)
Please read comments
Sub KOPIA1()
    Dim NAME_OF_SHEET As String
    NAME_OF_SHEET = Application.ActiveSheet.Name ' name of sheet which will be saved as .csv
    Workbooks.Add
    ActiveWorkbook.SaveAs ("C:\Documents and Settings\KAZIKL\Pulpit\" & NAME_OF_SHEET & ".xls") ' C:\Documents and Settings\KAZIKL\Pulpit\ path to temporary .xls file (You should change this patch)
    Windows("KAZIK1.xls").Activate ' here put name of your file
    Sheets(NAME_OF_SHEET).Select
    Sheets(NAME_OF_SHEET).Copy Before:=Workbooks(NAME_OF_SHEET & ".xls").Sheets(1)
    Sheets(Array("Arkusz1", "Arkusz2", "Arkusz3")).Select ' in English Sheet1, Sheet2 and Sheet3
    Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
    ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\KAZIKL\Pulpit\" & NAME_OF_SHEET & ".csv", FileFormat:=xlCSV 'C:\Documents and Settings\KAZIKL\Pulpit\ path to .csv file (You should change this patch)
    ActiveWorkbook.Save
    ActiveWindow.Close
    Windows("KAZIK1.xls").Activate
    Kill ("C:\Documents and Settings\KAZIKL\Pulpit\" & NAME_OF_SHEET & ".xls") 'C:\Documents and Settings\KAZIKL\Pulpit\ path to temporary .xls file (You should change this patch)
    Application.DisplayAlerts = True
End Sub

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Values to Excel Sheet from Sub form ayazhoda Access VBA 3 June 12th, 2012 04:18 PM
Removing a specific sheet from a loop in Excel sibo32000 Excel VBA 1 February 9th, 2007 01:54 PM
Export to Excel as .csv file from JSP naheedv Reporting Services 3 November 30th, 2006 08:04 AM
CSV and Excel sheet import itHighway Classic ASP Professional 0 August 5th, 2006 09:25 AM
Export Image to Excel sheet haribala.raj General .NET 0 September 14th, 2005 12:56 AM



All times are GMT -4. The time now is 02:21 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.