Export visible cells in a range as a csv

TheEmpeorer

New Member
Joined
Sep 12, 2016
Messages
2
Hi Mr. Excel geniuses,

Thanks for taking the time to help.

I wrote a function (copied down below) a while ago to export a range as a csv and now I want to modify it. Because I'm filtering a list and I only want the items that are filtered to be exported, I need it to do the same thing except I would like it to skip non-visible cells. Any ideas on how to modify this? All help is appreciated.

Thanks again.

Code:
Function ExportRangeToCSV(FilePath As String, Source As Range)

    Dim arrText() As String
    Dim sMsg      As String
    Dim i         As Double
    Dim rCell     As Range
    Dim rSubCell  As Range
    
    Const Delimiter As String = ","
    
    Application.Goto Reference:=Source
    
    ReDim arrText(1 To Selection.Rows.Count)


    For Each rCell In Selection.Resize(Selection.Rows.Count, 1)
    
        i = i + 1


        On Error Resume Next
        For Each rSubCell In Intersect(rCell.EntireRow, Selection)
            arrText(i) = arrText(i) & rSubCell.Value & Delimiter
            If Err.Number = "13" Then
                arrText(i) = arrText(i) & rSubCell.Text & Delimiter
                Err.Number = vbNull
            End If
        Next rSubCell
        On Error GoTo 0
        
        If Right(arrText(i), Len(Delimiter)) = Delimiter Then
            arrText(i) = Left(arrText(i), Len(arrText(i)) - Len(Delimiter))
        End If
    
    Next rCell


    Close #1
    Open FilePath For Output As #1
        For i = 1 To UBound(arrText)
            Print #1, arrText(i)
        Next i
    Close #1


    sMsg = "File saved as:" & vbCrLf & FilePath
    MsgBox sMsg, vbOKOnly, "CSV File"


    Application.Goto Reference:=Range("A1")


End Function
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Never mind, I got a solution.

For anyone interested I just copied out the filtered rows using ​.SpecialCells(xlCellTypeVisible) and then ran my old function on the result.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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