Excel表格网

excel怎么用宏批量处理条件格式?

135 2023-12-14 09:24 admin   手机版

把你要设置该条件格式的文件都放在一个目录,然后在该目录下新建一个工作薄,并打开,按ALT+F11打开VBA编辑器,,在左边列表栏点击鼠标右键插入一个模块,粘贴以下代码到右边窗口,

保存然后执行该代码. 这个目录下所有excel文件的第一个表格都会按你的要求设置了条件格式

注意:文件为excel 2003版扩展名是xls,

代码如下:

Sub Macro1() Dim FileName As String Dim WB As Workbook FileName = Dir(ThisWorkbook.Path & "*.xls") If FileName <> "" Then Do If FileName <> ThisWorkbook.Name Then Set WB = Workbooks.Open(ThisWorkbook.Path & "" & FileName) With WB.Sheets(1) For i = 0 To 21 str1 = "=" & Range("e1").Offset(0, i).Address str2 = "=" & Range("e2").Offset(0, i).Address With .Range("E3:E3000").Offset(0, i) .FormatConditions.Delete .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, Formula1:=str1, Formula2:=str2 .FormatConditions(1).Interior.ColorIndex = 3 End With Next End With WB.Save WB.Close End If FileName = Dir Loop While FileName <> "" End If End Sub

如果能下载附件 ,直接把这个文件放到你要设置条件格式的目录中,执行Macro1 宏.

顶一下
(0)
0%
踩一下
(0)
0%
相关评论
我要评论
用户名: 验证码:点击我更换图片
上一篇:返回栏目