7

我正在寻找一种将文本插入单元格背景的方法,以便我仍然可以在该文本之上输入数字 - 类似于水印,除了单个单元格。有什么方法可以做到这一点,最好不使用宏(但也对这些解决方案开放)?

4

4 回答 4

10

与 Andrews post 类似,这是 VBA 版本,可以正确格式化形状并允许直接选择单元格。

在此处输入图像描述

代码模块:

Sub watermarkShape()
Const watermark As String = "watermark"
Dim cll As Range
Dim rng As Range
Dim ws As Worksheet
Dim shp As Shape

    Set ws = Sheet1
    Set rng = ws.Range("A1:F10") 'Set range to fill with watermark

    Application.ScreenUpdating = False

    For Each shp In ws.Shapes
        shp.Delete
    Next shp

    For Each cll In rng

        Set shp = ws.Shapes.AddShape(msoShapeRectangle, 5, 5, 5, 5)

        With shp
            .Left = cll.Left
            .Top = cll.Top
            .Height = cll.Height
            .Width = cll.Width

            .Name = cll.address
            .TextFrame2.TextRange.Characters.Text = watermark
            .TextFrame2.TextRange.Font.Name = "Tahoma"
            .TextFrame2.TextRange.Font.Size = 8
            .TextFrame2.VerticalAnchor = msoAnchorMiddle
            .TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
            .TextFrame2.WordWrap = msoFalse
            .TextFrame.Characters.Font.ColorIndex = 15
            .TextFrame2.TextRange.Font.Fill.Transparency = 0.35

            .Line.Visible = msoFalse
'            Debug.Print "'SelectCell (""" & ws.Name & """,""" & cll.address & """)'"
            .OnAction = "'SelectCell """ & ws.Name & """,""" & cll.address & """'"

            With .Fill
                .Visible = msoTrue
                .ForeColor.ObjectThemeColor = msoThemeColorBackground1
                .Transparency = 1
                .Solid
            End With

        End With


    Next cll

    Application.ScreenUpdating = True
End Sub

Sub SelectCell(ws, address)
    Worksheets(ws).Range(address).Select
End Sub

更新:

下面的示例将单元格地址的水印分配给奇数行,并将偶数行保留为常量watermark。这是基于我的评论的一个例子,任何单元格都可以根据你想要的任何条件分配任何水印文本。

在此处输入图像描述

Option Explicit

Sub watermarkShape()
Const watermark As String = "watermark"
Dim cll As Range
Dim rng As Range
Dim ws As Worksheet
Dim shp As Shape

    Set ws = Sheet1
    Set rng = ws.Range("A1:F10") 'Set range to fill with watermark

    Application.ScreenUpdating = False

    For Each shp In ws.Shapes
        shp.Delete
    Next shp

    For Each cll In rng

        Set shp = ws.Shapes.AddShape(msoShapeRectangle, 5, 5, 5, 5)

        With shp
            .Left = cll.Left
            .Top = cll.Top
            .Height = cll.Height
            .Width = cll.Width

            .Name = cll.address
            If cll.Row Mod 2 = 1 Then
                .TextFrame2.TextRange.Characters.Text = cll.address
            Else
                .TextFrame2.TextRange.Characters.Text = watermark
            End If
            .TextFrame2.TextRange.Font.Name = "Tahoma"
            .TextFrame2.TextRange.Font.Size = 8
            .TextFrame2.VerticalAnchor = msoAnchorMiddle
            .TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
            .TextFrame2.WordWrap = msoFalse
            .TextFrame.Characters.Font.ColorIndex = 15
            .TextFrame2.TextRange.Font.Fill.Transparency = 0.35

            .Line.Visible = msoFalse
'            Debug.Print "'SelectCell (""" & ws.Name & """,""" & cll.address & """)'"
            .OnAction = "'SelectCell """ & ws.Name & """,""" & cll.address & """'"

            With .Fill
                .Visible = msoTrue
                .ForeColor.ObjectThemeColor = msoThemeColorBackground1
                .Transparency = 1
                .Solid
            End With

        End With


    Next cll

    Application.ScreenUpdating = True
End Sub

Sub SelectCell(ws, address)
    Worksheets(ws).Range(address).Select
End Sub
于 2013-08-13T00:56:59.153 回答
9

您可以使用自定义数字格式(选择单元格,按 Ctrl+1,数字格式,自定义)来指定当单元格值为 0 时要显示的浅灰色文本 -Color15制作漂亮的水印颜色:

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

水印单元格

没有杂乱的形状,没有 VBA,当实际填满值时,水印消失了。

如果您绝对需要在 VBA 中执行此操作,那么您可以轻松编写一个基于某些参数构建格式字符串的函数:

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

然后你可以这样做:

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

您仍然可以根据需要为正/负值提供自定义格式;唯一的事情是0为“无价值”保留并触发水印。

myCell.NumberFormat = BuildWatermarkFormat("Please enter a value", "[Blue]#,##0.00_)", "[Red](#,##0.00)")
myCell.Value = -25
于 2017-06-30T19:32:57.233 回答
7
  • 选择要制作背景的单元格。
  • 单击“插入”并在该位置插入一个矩形形状。
  • 右键单击形状 - 选择“格式化形状”
  • 转到“填充”并选择“图片或纹理填充”
  • 转到“从文件插入”选项
  • 选择要制作水印的图片
  • 图片会出现在矩形的地方
  • 现在单击图片“右键单击”并选择格式化图片
  • 转到“填充”并根据需要增加透明度,使其看起来像“水印”或浅色背景
  • 这也会被打印出来。

取自这里

于 2013-08-12T21:49:40.677 回答
-1

在任意位置的单元格中键入文本。复制它,它将保存在剪贴板上。在任何地方插入一个矩形。右键单击并选择“发送到后面”。这将确保它在后台。右键单击并“格式化形状”。执行选项卡“填充”并单击“图片或纹理填充”。在“插入自”处选择“剪贴板”。现在,您复制到剪贴板上的任何文本都将呈矩形。调整形状以适合所需的单元格。随心所欲地进行调整,例如删除矩形线、添加阴影、更改字体、删除背景等。

于 2014-04-29T04:38:08.887 回答