gpt4 book ai didi

excel - 将文本插入单元格的背景

转载 作者:行者123 更新时间:2023-12-02 00:07:05 27 4
gpt4 key购买 nike

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

最佳答案

与安德鲁斯帖子类似,这是 VBA 版本,可以正确设置形状格式,并且还允许直接选择单元格。

enter image description here

代码模块:

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。这是基于我的评论的示例,即可以根据您想要的任何条件为任何单元格分配任何水印文本。

enter image description here

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

关于excel - 将文本插入单元格的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com