I
I
iminby2018-02-08 03:52:06
Macros
iminby, 2018-02-08 03:52:06

How do I add quotes to text fields that have a space in an Excel save to CSV macro?

Tell me, what condition should be added in this macro so that quotes are added only to text strings that contain more than one word:


Sub SaveAsCSVinQuotes()
Dim r As Range, c As Range, s As String
s = Application.GetSaveAsFilename(, "CSV Files (*.csv),*.csv,All Files (*.*),*.*", , "Save as CSV in quotes")
If s = "False" Then Exit Sub
Open s For Output As #1
For Each r In ActiveSheet.UsedRange.Rows
s = ""
For Each c In r.Cells
s = s & " ;" & """" & c & """"
Next
Print #1, Mid$(s, 2)
Next
Close #1
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kiselyov, 2018-02-12
@zamboga

=ЕСЛИ(ДЛСТР(СЖПРОБЕЛЫ(A2))=0;0;ДЛСТР(СЖПРОБЕЛЫ(A2))-ДЛСТР(ПОДСТАВИТЬ(A2;" ";""))+1)

This formula determines the number of words in a cell. If there is a space, then the number of words => 2.
Translate into English. language and insert into your loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question