Converting a Range of Cells into Comma-Separated Values

Written by Allen Wyatt (last updated May 9, 2026)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365


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

Resetting Paragraph Formatting

Tired of the formatting used in a paragraph? One way to "start over" is to make sure that the formatting is reset to its ...

Discover More

Understanding Underlines

Excel provides a variety of underlining styles you can use when you need to underline information within a cell. Here's ...

Discover More

Changing Elements in Lots of Charts at One Time

Got a bunch of charts that you need to make formatting changes in? You can use a macro (or two) to apply the formatting ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Appending to a Non-Excel Text File

Does your macro need to add information to the end of a text file? This is called appending and is done using the ...

Discover More

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

Pulling Filenames into a Worksheet

You can use Excel for all types of data processing. You may want to work with filenames in a worksheet, but the first ...

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 2 + 6?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.