Я не был уверен, что приведенный ниже код действительно работает, но он должен.По сути, я минимизировал количество проверок условий Range
.Минимизируя количество обращений к свойствам range
, я тем самым минимизирую количество вызовов в Excel, что ускоряет процесс.Я также использовал переменные boolean
, чтобы VBA не слишком часто ссылалась на объекты.
Sub ColorTimes()
Dim b9Union As Boolean, b930Union As Boolean, b7Union As Boolean, bContinue As Boolean
Dim i As Integer
Dim rColorNone As Range, rColors49BF As Range, rColors49BE As Range
Dim rLoop As Range, r7A As Range, r9A As Range, r930A As Range
Dim wks3 As Worksheet
'Initialize variables
Set wks3 = Sheet3
With wks3
Set rColorNone = .Range("A5:A22")
Set rColors49BE = .Range("B5:E22")
Set rColors49BF = .Range("B5:F22")
End With
i = -1: bUnion = False
'Loop through range in column A.
For Each rLoop In rColorNone
i = i + 1
'Check column A first, VBA automatically checks
'all values in AND statements, so you need to split them up.
If rLoop.Interior.ColorIndex = xlColorIndexNone Then
bContinue = True
'Check first conditions, if true then don't bother checking the next conditions.
If rColors49BF.Resize(1).Offset(i).Interior.ColorIndex = 46 Then
Time7A9A r7A, r9A, wks3, b7Union, b930Union, i + 5
b7Union = True: b930Union = True
bContinue = False
End If
If bContinue Then
If rColors49BE.Resize(1).Offset(i).Interior.ColorIndex = 46 Then
Time7A9A r7A, r9A, wks3, b7Union, b9Union, i + 5
b7Union = True: b9Union = True
End If
End If
End If
Next rLoop
If Not r7A Is Nothing Then r7A = "7 a"
If Not r9A Is Nothing Then r9A = "9 a"
If Not r930A Is Nothing Then r930A = "9:30 a"
End Sub
Private Sub Time7A9A(ByRef r7A As Range, ByRef r9A As Range, ByRef wks As Worksheet _
, ByVal b7Union As Boolean, b9Union As Boolean, ByVal iRow As Integer)
If b7Union Then
Set r7A = Union(r7A, wks.Cells(iRow, 3))
Else
Set r7A = wks.Cells(iRow, 3)
End If
If b9Union Then
Set r9A = Union(r9A, wks.Cells(iRow, 4))
Else
Set r9A = wks.Cells(iRow, 4)
End If
End Sub