Underlining a Value in a Variable

[Closed]
Report
-
Posts
17186
Registration date
Sunday June 8, 2008
Status
Contributor
Last seen
October 26, 2021
-
Hello Guys,

I have a variable which contains some value. I would like to underline it before printing it into a cell. I have used the following lines of code but does not work. Any help would be appreciated :)

Sub underline()

Dim x As Variant
x = "Hello"
x.Font.underline = True

End Sub

Thanks :)

2 replies

Posts
2819
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
October 25, 2021
487
Hi AdiK,

You can't apply format to a variable, but you can to a cell.

Sub underline()
Dim x As Variant

x = "Hello"
Range("A1") = x
Range("A1").Font.underline = True

End Sub


Best regards,
Trowa

Posts
17186
Registration date
Sunday June 8, 2008
Status
Contributor
Last seen
October 26, 2021
5
I would rather try something such as :
Range("A3").Font.Underline = xlUnderlineStyleSingle

A good method to learn about that is to record a macro while manually changing the format of a cell : you will see in the recorded VBA code how to programmatically access those attributes.