Results 1 to 4 of 4

Thread: Save File In CSV Format VBA

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Question Save File In CSV Format VBA

    I am using EXCEL 2007 in windows 7.

    I have a simple file with 2 worksheets. Sheet1 & Sheet2.

    I need to do these things via VBA.

    Copy Sheet2!A1:C500 Paste Special Values only.
    Delete Sheet2!1:1 row 1 whole row
    Delete Sheet1
    Ask an Inputbox for the file name
    Save As file in CSV format in the below location
    C:\Users\RajKum\My Documents\Converted.

    Needs to create Converted folder, if not exists.

    Appreciate all your help

    Raj

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi Raj Kumar,

    Welcome to ExcelFox!

    Try

    Code:
    Sub SaveAsCSV()
        
        Dim CSVFileName     As String
        Dim wbkActive       As Workbook
        Dim wbkNew          As Workbook
        Dim i               As Long
        Dim blnFolderExists As Boolean
        Dim FilePath        As String
        
        FilePath = "C:\Users\RajKum\My Documents\Converted\"
        
        With Application
            .ScreenUpdating = 0
            .DisplayAlerts = 0
        End With
        
        Set wbkActive = ThisWorkbook
        Set wbkNew = Workbooks.Add
        
        wbkNew.Worksheets(1).Range("a1:c499").Value = wbkActive.Worksheets(2).Range("a2:c500").Value
        
        CSVFileName = Application.InputBox("Enter the New CSV File Name", Type:=2)
        
        Application.DisplayAlerts = 0
        For i = wbkNew.Worksheets.Count To 2 Step -1
            wbkNew.Worksheets(i).Delete
        Next
        
        blnFolderExists = CBool(Len(Dir(FilePath, vbDirectory)))
        
        If Not blnFolderExists Then
            MkDir FilePath
        End If
        
        If Right$(FilePath, 1) <> "\" Then FilePath = FilePath & Application.PathSeparator
        
        wbkNew.SaveAs Filename:=FilePath & CSVFileName, FileFormat:=6 '"CSV"
        wbkNew.Close
        
        Set wbkNew = Nothing
        Set wbkActive = Nothing
        
        With Application
            .DisplayAlerts = 1
            .ScreenUpdating = 1
        End With
    
    End Sub

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0
    Thank you, Admin.

    Works like a charm...!

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi Raj Kumar,

    You are welcome !

    Glad I could help you.

Similar Threads

  1. Save Worksheets As New File To Specific Folder
    By k0st4din in forum Excel Help
    Replies: 18
    Last Post: 06-08-2013, 04:24 PM
  2. Replies: 1
    Last Post: 03-07-2013, 11:42 AM
  3. Save Excel 2010 File In CSV Format VBA
    By mag in forum Excel Help
    Replies: 7
    Last Post: 01-08-2013, 07:16 PM
  4. Importing a csv File to a range
    By SDruley in forum Excel Help
    Replies: 21
    Last Post: 11-20-2012, 04:54 PM
  5. Replies: 12
    Last Post: 08-19-2012, 06:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •