vba Visual Basic 宏 - 更改特定文本的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16627054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual basic macro- changing the colour of specific text
提问by Thomas
Im very new to visual basic and macros. what i have been trying to do is create a macro that will look through the whole document and check to see if any of the font is red;if it is red then i want to change the red font to a white font.
我对视觉基础和宏很陌生。我一直在尝试做的是创建一个宏,它将查看整个文档并检查是否有任何字体是红色的;如果它是红色的,那么我想将红色字体更改为白色字体。
I know my code is wrong but can anyone tell me what i am doing wrong?
我知道我的代码是错误的,但谁能告诉我我做错了什么?
Sub red()
If Font.Color =wdColorRed Then
Font.Color = -603914241
End Sub
回答by jhoe
You can use the following which is fairly quick (no looping required for each sheet).
您可以使用以下相当快的方法(每张纸都不需要循环)。
Sub Macro1()
Application.ScreenUpdating = False 'disable the screen from updating, i.e, avoid excel redrawing the screen after each color change we make
Application.FindFormat.Font.Color = 255
Application.ReplaceFormat.Font.Color = 16777215
Cells.Select
Selection.Replace What:="", Replacement:="", MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Application.ScreenUpdating = True 'enable screen updating
End Sub
Font colors can be a little tricky. So to find out what color you want, select a cell and change the color to a color you need to know the number for. Then go to your developer screen and View -> Immediate Window (or hit Ctrl+G). Then in the Immediate window (should now be at the bottom of your screen, with the cell that has you color you want to know still selected, type
字体颜色可能有点棘手。因此,要找出您想要的颜色,请选择一个单元格并将颜色更改为您需要知道其编号的颜色。然后转到您的开发人员屏幕并查看 -> 立即窗口(或按 Ctrl+G)。然后在“立即”窗口中(现在应该在屏幕底部,具有您想要知道的颜色的单元格仍处于选中状态,键入
? Selection.Font.Color
and this will give you the color you are interested in. Then put those numbers in Application.Find/ReplaceFormat.Font.Color above.
这会给你你感兴趣的颜色。然后把这些数字放在上面的 Application.Find/ReplaceFormat.Font.Color 中。
This will work for the sheet selected, you can simply throw this in a loop and iterate over all the sheets in a workbook to change them all.
这将适用于选定的工作表,您可以简单地将其放入循环中并迭代工作簿中的所有工作表以将它们全部更改。
回答by Santosh
Is it what you are looking for ?
这是您要找的吗?
Copied from here@Todd Main Answer.
从这里复制@Todd 主要答案。
Sub ChangeColorWithReplace()
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorRed
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = -603914241
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub