Visual Basic宏-更改特定文本的颜色 [英] Visual basic macro- changing the colour of specific text

查看:167
本文介绍了Visual Basic宏-更改特定文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对视觉基础和宏非常陌生.我一直想做的是创建一个宏,该宏将在整个文档中查看并检查是否有任何字体是红色的;如果它是红色的,那么我想将红色字体更改为白色字体.

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

推荐答案

您可以使用以下内容,这是相当快的(每个工作表都不需要循环).

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

字体颜色可能会有些棘手.因此,要找出所需的颜色,请选择一个单元格,然后将其更改为需要知道其编号的颜色.然后转到开发人员屏幕,然后单击查看->立即窗口(或按Ctrl + G).然后在立即"窗口(现在应位于屏幕底部,仍要选择要显示颜色的单元格中,键入

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

? Selection.Font.Color

这将为您提供您感兴趣的颜色.然后将这些数字放在上面的Application.Find/ReplaceFormat.Font.Color中.

and this will give you the color you are interested in. Then put those numbers in Application.Find/ReplaceFormat.Font.Color above.

这将适用于选定的工作表,您只需将其循环放入并遍历工作簿中的所有工作表即可更改所有工作表.

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.

这篇关于Visual Basic宏-更改特定文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆