Logan
Logan

Reputation: 329

Entering '=' as the first character in a cell

I have a VBA macro that runs and often needs to have "=" as the first character in a cell. Cells are filled with such values as "= Domestic", "<> Domestic", etc.

Right now I am replacing "=" with "IS" as a quickfix, but I would like it if I could keep the equal sign, for consistency with cells that use symbols such as "<>". When I try, Excel thinks it is a function with the wrong syntax. I am using Cells(row, col).Value = x, where x is the string that may or may not start with "=", to enter the data.

Upvotes: 5

Views: 8864

Answers (2)

ray
ray

Reputation: 8699

This is an alternate method

Sub Test()
    Dim myRange As Range

    Set myRange = Range("C:C") 'Column "C"
    myRange.NumberFormat = "@" 'Set cell format of range to plain text
    myRange.Cells(1, 1) = "= Domestic"

End Sub

Compared to manji's answer, it provides no performance or size enhancement...just different.

Upvotes: 6

manji
manji

Reputation: 47978

Add a single quote ' at the start of the string:

Cells(row, col).Value = "'" & x

and you will get what you want.

Upvotes: 17

Related Questions