user1283776
user1283776

Reputation: 21784

Enter value as text to cell using VBA even if value starts with =?

I get an error on the following expression:

wksWrite.Cells(lWriteRow, "F") = Trim(wksRead.Cells(lReadRow, 2))

because Trim(wksRead.Cells(lReadRow, 2)) evaluates to:

=SKYDDSFOLIE FÖR SR-72 5424

I guess Excel evaluates that as a formula and doesn't allow VBA to enter it. But I want to enter that value as text. I tried adding:

    wksWrite.Cells(lWriteRow, "B").NumberFormat = "@"
    wksWrite.Cells(lWriteRow, "F").Value = CStr(Trim(wksRead.Cells(lReadRow, 2)))

But I still get the same error. How can I force Excel to interpret the value as text and accept it?

Upvotes: 2

Views: 1163

Answers (1)

PaulFrancis
PaulFrancis

Reputation: 5819

You can add a Single Quotes before the value making it treat as a String,

wksWrite.Cells(lWriteRow, "F") = "'" & Trim(wksRead.Cells(lReadRow, 2))

Upvotes: 2

Related Questions