Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding single quotes to a cell using VBA

I am trying to add single quotes to values in a column by using Chr(39), but I am only getting the second quote.
e.g. I want 'value' but I am getting value'

But if I use double quotes by using Chr(34) it works, and I get "value"

Sub AddQuote()
Dim myCell As Range
    For Each myCell In Selection
        If myCell.Value <> "" Then
            myCell.Value = Chr(39) & myCell.Value & Chr(39)
        End If
    Next myCell
End Sub
like image 390
Kiranshell Avatar asked Jul 13 '12 18:07

Kiranshell


1 Answers

just add another single quote:

Sub AddQuote()
Dim myCell As Range
    For Each myCell In Selection
        If myCell.Value <> "" Then
            myCell.Value = Chr(39) & Chr(39) & myCell.Value & Chr(39)
        End If
    Next myCell
End Sub
like image 79
whytheq Avatar answered Sep 26 '22 11:09

whytheq