Vba Word宏,用于根据不同列中的值更改特定列中负数的颜色
提前感谢您的回复。Vba Word宏,用于根据不同列中的值更改特定列中负数的颜色,vba,ms-word,Vba,Ms Word,提前感谢您的回复。 我正在做一些报告的介绍。定期报告从不同的软件导入Word模板。对于所有表格和每一行,我只想在第3列有特定文本的情况下更改第14列中负数的颜色。 不幸的是,我必须使用Word模板来完成此操作。似乎宏是我唯一的选择,所以我尝试从我在网上找到的不同宏中找到一些东西: Dim varColumn As Column Dim clColumn As Column Dim cCell As Variant Set clColumn = Selection.Columns(3)
我正在做一些报告的介绍。定期报告从不同的软件导入Word模板。对于所有表格和每一行,我只想在第3列有特定文本的情况下更改第14列中负数的颜色。 不幸的是,我必须使用Word模板来完成此操作。似乎宏是我唯一的选择,所以我尝试从我在网上找到的不同宏中找到一些东西:
Dim varColumn As Column
Dim clColumn As Column
Dim cCell As Variant
Set clColumn = Selection.Columns(3)
Set varColumn = Selection.Columns(14)
With clColumn
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.text = "value"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
Selection.MoveRight Unit:=wdCell, Count:=11
End If
If cCell < 0 Then
Selection.Font.color = wdColorRed
End If
Loop
End With
End Sub
Dim varColumn As Column
Dim clColumn As Column
Dim-cCell作为变体
Set clColumn=Selection.Columns(3)
Set varColumn=Selection.Columns(14)
使用clColumn
和…一起找
.ClearFormatting
.Replacement.ClearFormatting
.text=“值”
.Replacement.text=“”
.Forward=True
.Wrap=wdFindStop
.Format=False
.MatchWildcards=True
.执行
以
找,找,找到
如果.Information(wdWithInTable)=True,则
Selection.MoveRight单位:=wdCell,计数:=11
如果结束
如果cCell<0,则
Selection.Font.color=wdColorRed
如果结束
环
以
端接头
我认为宏需要行来重复搜索。请参见循环之前添加的两行
With Selection
.HomeKey Unit:=wdStory 'Starts at the beginning, to search all tables.
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "value"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True And _
.Cells(1).ColumnIndex = 3 Then 'Confirms it's in the 3rd column.
.MoveRight Unit:=wdCell, Count:=11
End If
If .Range < 0 Then
.Font.Color = wdColorRed
End If
.Collapse wdCollapseEnd 'Collapses the selection to no characters.
.Find.Execute 'Searches again from the current selection point.
Loop
End With
与选择
.HomeKey单位:=wdStory'从开头开始,用于搜索所有表。
和…一起找
.ClearFormatting
.Replacement.ClearFormatting
.Text=“值”
.Replacement.Text=“”
.Forward=True
.Wrap=wdFindStop
.Format=False
.MatchWildcards=True
.执行
以
找,找,找到
If.Information(wdWithInTable)=True和_
.Cells(1).ColumnIndex=3然后“确认它在第3列中”。
.MoveRight单位:=wdCell,计数:=11
如果结束
如果.Range<0,则
.Font.Color=wdColorRed
如果结束
.Collapse wdCollapseEnd'将所选内容折叠为无字符。
.Find.Execute'从当前选择点再次搜索。
环
以
试试:
如果表格可能有垂直合并的单元格,请更改:
If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
致:
你的问题是什么?什么不起作用?嗨,麦克,我想用你以前的宏来得到我需要的结果。你发布的这个解决方案在我去掉垂直合并的单元格后就起作用了,所以非常感谢你。如果我的问题太基本,如果我的宏太乱,我会道歉。我确实买了一本关于这个主题的书,但在我有机会阅读这本书之前,我需要一个工作宏。一旦你找到了正确的方法,有些事情会变得更加清晰。再次感谢。@Tom看到了我关于如何处理带有垂直合并单元格的表格的更新答案。您好,这个解决方案也很有效。拥有两种不同的工作解决方案对像我这样仍在学习编写宏的人有很大帮助。非常感谢。
If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
If Split(.Tables(1).Cell(.Cells(1).RowIndex, 3).Range.Text, vbCr)(0) = "specified text" Then