把你要设置该条件格式的文件都放在一个目录,然后在该目录下新建一个工作薄,并打开,按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 宏.
追问麻烦问一下我只要不符合条件的字体变红色,单元格不用变色,这样空白的单元格也不用变色了。需要改哪里?谢谢
追答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).Range("E3:Z3000")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=IF(RC<>"""",IF(AND(RC>=R1C,RC<=R2C),0,1))"
.FormatConditions(1).Interior.ColorIndex = 3
End With
WB.Save
WB.Close
End If
FileName = Dir
Loop While FileName <> ""
End If
End Sub