前言
怎样快速批量实现Excel的条件格式?第一反应是VBA实现,经过查找,将VBA实现条件格式代码记录。
实现效果如下:
将文件夹下面的全部Excel文件设置相同的条件格式。
本次的难点:如果条件格式是【等于】1、2、3、4、5、6等多个条件,但是Excel条件格式中没有或条件,需要设置循环条件达到效果。
代码
项目gitee地址,可以下载案例测试。
Sub 条件格式()
Dim i As Integer
Dim j As Integer
Dim myname As String
Dim mynames As String
Set mysheet2 = ThisWorkbook.Worksheets("Sheet2")
j = mysheet2.UsedRange.Rows.Count
For i = 2 To j
mynames = "=" & Chr(34) & Sheet2.Cells(i, 1) & Chr(34)
Sheets("门店补货建议").Select
Columns("D:D").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=mynames
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'MsgBox myname
'MsgBox mynames
Next
End Sub
Sub 批量条件格式()
Dim wb As Excel.Workbook
x = ThisWorkbook.Path & "\"
f = Dir(x & "" & "*xls")
Do While f <> ""
If f <> ThisWorkbook.Name Then
Set wb = Workbooks.Open(x & "" & f)
Call 条件格式
wb.Save
wb.Close
End If
f = Dir
Loop
End Sub
由于对VBA不熟悉,如有错误,请提醒谢谢!