Search code examples
excelquotation-marksvba

Delimited List with Quotation Marks for SQL query


I have a cheat sheet I use to for turning a list of orders in a comma delimited list. I've been asked to duplicate it, but instead of simply returning the values from the list, I've been asked to place them in quotation marks for a SQL query a coworker. So a list like: A B C Should be returned as "A","B","C". Below is the code I have for creating the CSV list.

Function csvRange(myRange As Range)
Dim csvRangeOutput
Dim entry As Variant
For Each entry In myRange
    If Not IsEmpty(entry.Value) Then
        csvRangeOutput = csvRangeOutput & entry.Value & ","
    End If
Next
csvRange = Left(csvRangeOutput, Len(csvRangeOutput) - 1)
End Function

I'm totally new to VBA, but I tried playing with the code to figure it out myself. Adding more quotes, using the tilde symbol wasn't working. That basically exhausted my toolkit. Thanks for any help.


Solution

  • Try...

    If Not IsEmpty(entry.Value) Then
        csvRangeOutput = csvRangeOutput & """" & entry.Value & """" & ","
    End If