Create new csv files for some sheets of my workbook

mark84

New Member
Joined
Jan 22, 2021
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Hi everybody,
I have a workbook with 10 sheets named Lom mar.xlsm.
I need a vba code to create a new .csv file for the Workbook sheets named "AX", "BY", "CZ", "DU" (a csv for "AX" sheet, a csv for "BY" sheet, a csv for "CZ" sheet and a csv for "DU" sheet).
All these new csv files should be saved in the same path of the workbook (C:\Users\xxxx\Desktop\Lom mar)
Every new csv should have the same name of the original sheet (AX.csv, BY.csv, .....) and for each csv I need to delete the first row of the original sheet (header).

Could you help me please?
Thanks you so much!

Regards

Marco
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
VBA Code:
Sub EachSheetAsCSV()
For Each sh In ThisWorkbook.Worksheets
    Dim F%, Rw As Range
        F = FreeFile
        Open ThisWorkbook.Path & "\" & sh.Name & ".csv" For Output As #F
    With Application
        For Each Rw In sh.UsedRange.Rows
            If Rw.Row > 1 Then Print #F, Join(.Index(Rw.Value2, 1, 0), ",")
        Next
    End With
        Close #F
Next sh
End Sub
 
Upvote 0
Thank you so much for your answer.
Your code is perfect but it creates a csv for each sheets of my workbook.
How can I edit that code to create a csv only for the sheets named "AX", "BY", "CZ", "DU"?

Regards
Marco
 
Upvote 0
One way:
VBA Code:
Sub EachSheetAsCSV()

For Each sh In ThisWorkbook.Worksheets
    If (sh.Name = "AX") Or (sh.Name = "BY") Or (sh.Name = "CZ") Or (sh.Name = "DU") Then
        Dim F%, Rw As Range
        F = FreeFile
        Open ThisWorkbook.Path & "\" & sh.Name & ".csv" For Output As #F
        With Application
            For Each Rw In sh.UsedRange.Rows
                If Rw.Row > 1 Then Print #F, Join(.Index(Rw.Value2, 1, 0), ",")
            Next
        End With
    End If
    
    Close #F
Next sh

End Sub
 
Upvote 0
With longer list of worksheets this way is better, I think:
VBA Code:
Sub EachSheetAsCSV()
Dim F%, Rw As Range
Dim V As Variant
V = Array("AX", "BY", "CZ", "DU")
For Each sh In ThisWorkbook.Worksheets
    If Not IsError(Application.Match(sh.Name, V, 0)) Then
        F = FreeFile
        Open ThisWorkbook.Path & "\" & sh.Name & ".csv" For Output As #F
        With Application
            For Each Rw In sh.UsedRange.Rows
                If Rw.Row > 1 Then Print #F, Join(.Index(Rw.Value2, 1, 0), ",")
            Next
        End With
        Close #F
    End If
Next sh
End Sub
However Joe4's codee of course works perfect as well.
 
Upvote 0
With longer list of worksheets this way is better, I think:
I agree. 4 is right around the cusp of where I would start to think about using an array too.
 
Upvote 0
Thank you so much KOKOSEK and Joe4!
It works very well.

My last request:
I'm using the code with ARRAY because I could work with more than 4 sheets in the future.
I obtain the csv output but it has the format with comma between the values.
Is is possible to have a csv output that doesn't have comma as list separator?
I attach the csv output with your code and the csv output that I'd want.
Could you help me in this last point?

Regards
Marco
 

Attachments

  • to be.PNG
    to be.PNG
    21.9 KB · Views: 4
  • as is with your code.PNG
    as is with your code.PNG
    12.4 KB · Views: 3
Upvote 0
I do not know how did you open this CSV but it should displays each value in separate cell.
 
Upvote 0
I do not know how did you open this CSV but it should displays each value in separate cell.
I opened it with Excel but all the values of each row are in a single cell, separated with comma.
What could be the problem?
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top