Search code examples
vbacustom-formatting

vba number format write specific text


i am pretty new in using VBA for Excel, but after some searching without result i guess i need some help from you.

I am trying to format a specific cell so that, if one condition is met, it shows the value from another cell, but preceded by a specific text, like "Constantin". So, the displayed result would be, for example Constantin 80.25 .

The code that i'm trying looks like this:

    If Cells(4, 1) < 0 Then

    With Range("A1")
    .NumberFormat = "'Constantin' 0.00"
    .Value = - Cells(4, 1)

    End With

    End If

I know the syntax is not right, this is my problem, i guess i can't find the right syntax. I hope I'm not bothering you with this, probably, simple question, but i just couldn't find what i needed. Thanks a lot


Solution

  • It looks like you don't need the word as part of the actual format, in which case something like this will suffice:

    If Cells(4, 1).Value < 0 Then
        Range("A1").Value = "Constantin " & (Cells(4, 1).Value * -1)
    End If
    

    If you really wanted to use a With block then:

    With Cells(4, 1)
        If .Value < 0 Then
            Range("A1").Value = "Constantin " & (.Value * -1)
        End If
    End With