Give a short sample of what the data in the range will be, what you want the
delimeter to be, and what you want the output file to look like.
I was wanting to use a comma as the delimiter and a double quote as
the string delimiter.
I've actually managed to apply the same principles as your my original
code and it works but it seems quite bloated and I'm sure there's a
more elegant way to do it. I'm more confused by why my previous
suggestion wouldn't work with the following data:-
Business Relationship Manager, Portfolio Manager, Project Manager,
Process Manager
Demand Manager, Clarity StudioTM, Resource Planner, Project Manager
IT Financial Manager, Demand Manager, Schedule Connect, Resource
Planner
IT Portfolio Manager, Financial Manager, Service Connect, Schedule
Connect
Open Workbench, Process Manager, Portfolio Manager, Service Connect
Process Manager, Project Manager, Clarity StudioTM, Portfolio Manager
Proejct Manager, Resource Planner, Demand Manager, Clarity StudioTM
Project Financial Manager, Schedule Connect, Financial Manager, Demand
Manager
Project Portfolio Manager, Service Connect, Process Manager, Financial
Manager
Resource Manager, Portfolio Manager, Project Manager, Process Manager
If you try this data with my original version all the entries end up
in one column. With my version it ends up as
"Business Relationship Manager","Portfolio Manager","Project
Manager","Process Manager"
"Demand Manager","Clarity StudioTM","Resource Planner","Project
Manager"
"IT Financial Manager","Demand Manager","Schedule Connect","Resource
Planner"
"IT Portfolio Manager","Financial Manager","Service Connect","Schedule
Connect"
"Open Workbench","Process Manager","Portfolio Manager","Service
Connect"
"Process Manager","Project Manager","Clarity StudioTM","Portfolio
Manager"
"Proejct Manager","Resource Planner","Demand Manager","Clarity
StudioTM"
"Project Financial Manager","Schedule Connect","Financial
Manager","Demand Manager"
"Project Portfolio Manager","Service Connect","Process
Manager","Financial Manager"
"Resource Manager","Portfolio Manager","Project Manager","Process
Manager"
Here is my final solution - the comments should explain how it works:-
Sub RNG2CSV(sWorkSheet As String, rng As Range, sFilename As String)
'sWorkSheet : Name of worksheet containing range as string
'rng : Range as range object of range to export
'sFilename : Full path to the CSV file to be exported
Dim StringDelimiter As String: StringDelimiter = """"
Dim sOutput As String
Dim sFname As String, lFnum As Long
Dim lRowF As Long: lRowF = rng.Row
Dim lRowL As Long: lRowL = lRowF + rng.Rows.Count - 1
Dim lColF As Long: lColF = rng.Column
Dim lColL As Long: lColL = lColF + rng.Columns.Count
Dim r As Long, c As Long
'Open a text file to write
sFname = sFilename
lFnum = FreeFile
Open sFname For Output As lFnum
'Loop through the rows
Dim ws As Worksheet: Set ws = Worksheets(sWorkSheet)
With ws
For r = lRowF To lRowL
'Loop through the cells in the rows
For c = lColF To lColL
If Len(ws.Cells(r, c)) = 0 Then
sOutput = sOutput & ","
Else
sOutput = sOutput & "," & StringDelimiter &
ws.Cells(r, c) & StringDelimiter
End If
Next c
'remove the last comma
sOutput = Left(sOutput, Len(sOutput) - 1)
sOutput = Right(sOutput, Len(sOutput) - 1)
'write to the file and reinitialize the variables
Print #lFnum, sOutput
sOutput = ""
' i = LBound(vaColPad)
Next r
End With
'Close the file
Close lFnum
End Sub