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.
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