gpt4 book ai didi

更改表格颜色的 VBA 宏

转载 作者:行者123 更新时间:2023-12-04 05:37:44 25 4
gpt4 key购买 nike

我有一个包含多个表格的 Word 文档。我希望能够选择表格(或表格中的单元格)并让表格中的每一行都以交替颜色着色。到目前为止,我已经创建了以下代码:

Sub ColorTable()
'
' ColorTable Macro
' Alternately colors cells.
'
Selection.Collapse Direction:=wdCollapseStart
If Not Selection.Information(wdWithInTable) Then
MsgBox "Can only run this within a table"
Exit Sub
End If
Dim RowCount, i, count, ColCount As Integer
RowCount = ActiveDocument.Tables(1).Rows.count
i = 0
ColCount = ActiveDocument.Tables(1).Columns.count
For i = 1 To RowCount
For count = 1 To ColCount
Selection.Shading.BackgroundPatternColor = RGB(184, 204, 228)
'light
Selection.MoveRight Unit:=wdCharacter, count:=1
Next count
Selection.MoveDown Unit:=wdLine, count:=1
For count = 1 To ColCount
Selection.Shading.BackgroundPatternColor = RGB(219, 229, 241)
'dark
Selection.MoveRight Unit:=wdCharacter, count:=1
Next count
Next i
End Sub

宏运行没有错误,但以对角线模式更改单元格颜色。我猜问题出在我的 for 循环中。

最佳答案

With tblNew
For i = 1 To .Rows.Count Step 2
.Rows(i).Shading.Texture = wdTexture10Percent
' or
.Rows(i).Shading.BackgroundPatternColor = RGB(219, 229, 241)
Next
End With

关于更改表格颜色的 VBA 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11704609/

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