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 July 31st, 2006, 02:07 PM
Registered User
 
Join Date: Jul 2006
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
 
Old August 1st, 2006, 09:44 AM
Registered User
 
Join Date: Jul 2006
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
 
Old August 1st, 2006, 12:55 PM
Registered User
 
Join Date: Jul 2006
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
 
Old August 1st, 2006, 01:41 PM
Authorized User
 
Join Date: Jul 2006
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

 
Old August 1st, 2006, 02:45 PM
Registered User
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you. that does help :)
 
Old August 1st, 2006, 03:04 PM
Authorized User
 
Join Date: Jul 2006
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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Values to Excel Sheet from Sub form ayazhoda Access VBA 3 June 12th, 2012 03: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 08:25 AM
Export Image to Excel sheet haribala.raj General .NET 0 September 13th, 2005 11:56 PM





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