前言

怎样快速批量实现Excel的条件格式?第一反应是VBA实现,经过查找,将VBA实现条件格式代码记录。

实现效果如下:

将文件夹下面的全部Excel文件设置相同的条件格式。

本次的难点:如果条件格式是【等于】1、2、3、4、5、6等多个条件,但是Excel条件格式中没有或条件,需要设置循环条件达到效果。

Excel_条件格式_等于.png 2021-05-11-15-28-57.mp4

代码

项目gitee地址,可以下载案例测试。

  1. Sub 条件格式()
  2. Dim i As Integer
  3. Dim j As Integer
  4. Dim myname As String
  5. Dim mynames As String
  6. Set mysheet2 = ThisWorkbook.Worksheets("Sheet2")
  7. j = mysheet2.UsedRange.Rows.Count
  8. For i = 2 To j
  9. mynames = "=" & Chr(34) & Sheet2.Cells(i, 1) & Chr(34)
  10. Sheets("门店补货建议").Select
  11. Columns("D:D").Select
  12. Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
  13. Formula1:=mynames
  14. Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
  15. With Selection.FormatConditions(1).Interior
  16. .PatternColorIndex = xlAutomatic
  17. .Color = 65535
  18. .TintAndShade = 0
  19. End With
  20. Selection.FormatConditions(1).StopIfTrue = False
  21. 'MsgBox myname
  22. 'MsgBox mynames
  23. Next
  24. 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不熟悉,如有错误,请提醒谢谢!