Neosonic
Neosonic

Reputation: 13

how to add html tags to the beginning of a cell in excell using VBA

I am trying to add the following code in front of a group of cells in a column in excel 2011.

<div class="cpt_product_description "><div>

i tried using this:

Sub Add2Formula()

' Add text

For Each c In Selection

c.Activate

ActiveCell.FormulaR1C1 = "test - " & ActiveCell.Formula

Next c

End Sub

--- this worked and added the "test - " in front of the cells - resulting in these expected results:

test - lknaslkndaslkdn

When i try to add the div html in the vba code - (where the "test -" is in the above code example) it gives me errors like "unexpected:exception" and it highlites the text red as if there's formatting issues in the vba editor.... am i missing something that allows for characters like < and spaces and " and such?

Any help would be greatly appreciated.

the final results should look like this:

<div class="cpt_product_description "><div>lknaslkndaslkdn

Upvotes: 1

Views: 708

Answers (1)

glh
glh

Reputation: 4972

Use your text as "<div class=""cpt_product_description ""><div>"

Eg

ActiveCell.FormulaR1C1 = "<div class=""cpt_product_description ""><div>" & ActiveCell.Formula

Added explanation

The " notes the start or end of a string in vba. If you need a " in the string the only way vba lets you do this is to use 2 e.g."".

Upvotes: 1

Related Questions