Insert text into the background of a cell

后端 未结 4 520
失恋的感觉
失恋的感觉 2020-12-15 01:23

I am looking for a way to insert text into the background of a cell, so that I can still enter numbers on top of that text - similar to a watermark except for an individual

4条回答
  •  -上瘾入骨i
    2020-12-15 01:56

    You can use a custom number format (select the cell(s), hit Ctrl+1, number formats, custom) to specify a light-grey text to display when the cell value is 0 - Color15 makes a nice watermark color:

    [Black]000000;;[Color15]"(order number)";@
    

    watermarked cells

    No messy shapes, no VBA, and the watermark disappears when the value is actually filled up.

    And if you absolutely need to do it in VBA, then you can easily write a function that builds the format string based on some parameters:

    Public Function BuildWatermarkFormat(ByVal watermarkText As String, Optional ByVal positiveFormat As String = "General", Optional ByVal negativeFormat As String = "General", Optional ByVal textFormat As String = "General") As String
        BuildWatermarkFormat = positiveFormat & ";" & negativeFormat & ";[Color15]" & Chr(34) & watermarkText & Chr(34) & ";" & textFormat
    End Function
    

    And then you can do:

    myCell.NumberFormat = BuildWatermarkFormat("Please enter a value")
    myCell.Value = 0
    

    And you can still supply custom formats for positive/negative values as per your needs; the only thing is that 0 is reserved for "no value" and triggers the watermark.

    myCell.NumberFormat = BuildWatermarkFormat("Please enter a value", "[Blue]#,##0.00_)", "[Red](#,##0.00)")
    myCell.Value = -25
    

提交回复
热议问题