Converting a Range of Cells into Comma-Separated Values

Written by Allen Wyatt (last updated May 9, 2026)

2

Harrison knows that he can save a worksheet as a CSV file. However, he only needs to convert the range C2:J51 into a CSV. He wonders if there is an easy way to do this.

There are several ways you can do this. A simple way is to open both the source workbook and a brand-new workbook. Copy the C2:J51 range and paste it into the new workbook. (You can use Paste Values to paste the information.) Then you can save the new workbook in CSV format. (Harrison said he knows how to do this.)

Another approach is to open the source workbook and a new Notepad document. In the workbook, enter the following formula into cell L2:

=TEXTJOIN(",",FALSE,C2:J2)

Copy this down to the range L3:L51, and you end up with strings of comma-separated values. Select the range L2:L51, copy it, and paste it within the Notepad document. Save it using a filename extension of CSV, and you have your desired CSV file.

There is one potential gotcha to this—if your source information includes cells that contain commas. In that case, you'll want to modify the formula so that it adds quote marks around the cell contents:

=CHAR(34)&TEXTJOIN(CHAR(34)&","&CHAR(34),FALSE,C2:J2)&CHAR(34)

If you need to do this sort of thing routinely, then you could benefit by using a macro to do the conversion and saving. Here's one that is rather robust:

Sub ExportRangeToCSV()
    Dim row As Range
    Dim cell As Range
    Dim line As String
    Dim output As String
    Dim f As Integer
    Dim s As String
    Dim sFile As Variant

    If TypeName(Selection) <> "Range" Then
        MsgBox "Please select a worksheet range first."
        Exit Sub
    End If

    For Each row In Selection.Rows
        line = ""
        For Each cell In row.Cells
            s = cell.Text   ' Change to .Value if underlying value is wanted
            If InStr(s, """") > 0 Then
                s = Replace(s, """", """""")
            End If

            If InStr(s, ",") > 0 Or InStr(s, """") > 0 Or _
              InStr(s, vbCr) > 0 Or InStr(s, vbLf) > 0 Then
                s = """" & s & """"
            End If
            line = line & s & ","
        Next cell
        If Len(line) > 0 Then
            line = Left(line, Len(line) - 1)
            output = output & line & vbCrLf
        End If
    Next row

    sFile = Application.GetSaveAsFilename( _
      InitialFileName:="export.csv", _
      FileFilter:="CSV Files (*.csv), *.csv")

    If sFile <> False Then
        f = FreeFile
        Open CStr(sFile) For Output As #f
        Print #f, output;
        Close #f
        MsgBox "CSV File Saved"
    Else
        MsgBox "File Not Saved"
    End If
End Sub

The macro assumes that you are selecting the cells you want to output in a CSV file. It correctly handles special characters within a cell, such as commas, tabs, newlines, and quote marks. It also allows the user to specify a filename and location for the file.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13982) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Specifying a Delimiter when Saving a CSV File in a Macro

You can, within a macro, save a workbook in several different file formats that are understood by Excel. However, you may ...

Discover More

Edits Cause Text to Switch to Odd Fonts

If you have problems with strange fonts showing up when you paste information into a document, it is helpful to ...

Discover More

Creating a Directory

Need to create a directory from within a macro? You can do it using a single command line, as detailed in this tip.

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Getting Rid of Empty Rows after Importing

Import data into a worksheet (or paste it there) and you may find that you end up with a group of blank cells you need to ...

Discover More

Correctly Saving Delimited Files

Delimited files are often created through Excel so that your data can be exported to other programs. If the delimited ...

Discover More

File Formats that Include Field Formats

If you import data into Excel that is created by other programs, you know that it can be bothersome to get your data ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 8?

2026-05-15 16:12:56

J. Woolley

Assuming Harrison has Copilot activated in Excel (currently requires Excel 365), here's another way he can "convert the range C2:J51 into a CSV."
First open the worksheet, then click the Copilot icon and request this:
"Convert the range C2:J51 to CSV."
Copilot will eventually reply with this:
"Here is the CSV output for range C2:J51:" followed by a text box containing the CSV.
Select the CSV in the text box and copy it to the clipboard (Ctrl+C), then paste it wherever it is needed (Ctrl+V).


2026-05-12 11:32:49

J. Woolley

Here's an alternate version of the Tip's macro based on its first suggestion:
"A simple way is to open both the source workbook and a brand-new workbook. Copy the C2:J51 range and paste it into the new workbook. (You can use Paste Values to paste the information.) Then you can save the new workbook in CSV format." In this case Excel automatically quotes text having special characters like comma, CR/LF, leading/trailing spaces, and quotation marks (which are doubled).

Sub ExportRangeToCSV2()
    Const myName = "ExportRangeToCSV2"
    Dim var As Variant, msg As String, rng As Range
    var = Selection.Address
    msg = "Select a contiguous range of cells for export to a CSV file:"
    On Error Resume Next
        Set rng = Application.InputBox(msg, myName, var, Type:=8)
    On Error GoTo 0
    If rng Is Nothing Then Exit Sub 'user clicked Cancel
    rng.Select
    If rng.Areas.Count > 1 Then
        MsgBox "Selected range is invalid", vbCritical, myName
        Exit Sub
    End If
    rng.Copy
    Workbooks.Add xlWBATWorksheet 'new ActiveWorkbook/ActiveSheet
    Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False
    var = Application.GetSaveAsFilename(myName, _
        "Comma Separated Values (*.csv), *.csv")
    If var = False Then 'user clicked Cancel
        ActiveWorkbook.Close False
        Exit Sub
    End If
    On Error Resume Next
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs var, xlCSV
        ActiveWorkbook.Close False
        Application.DisplayAlerts = True
        If Err = 0 Then
            msg = "Exported comma separated values from " _
            & rng.Address & " to" & vbLf & var & vbLf & vbLf _
            & "Do you want to open that file in Notepad?"
            If MsgBox(msg, vbYesNo, myName) = vbYes Then _
                Shell "Notepad.exe " & var, vbNormalFocus
        End If
    On Error GoTo 0
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.