Vba 同时循环一行和命名区域中的所有单元格

Vba 同时循环一行和命名区域中的所有单元格,vba,excel,named-ranges,Vba,Excel,Named Ranges,首先道歉,我是VBA新手 我想找到一种方法,我可以循环一行中的所有单元格,如果单元格是命名范围“目标”的一部分,我可以改变背景颜色 我最初是使用下面的代码来完成这项工作的,我会检查指定范围内的所有单元格,但速度太慢了,不实用。我希望通过将函数限制在活动行来加快宏的速度 我尝试过各种版本的Intersect,当我让它选择我想要查看的单元格时,我正在努力使用结果 Dim cell As Range For Each cell In Sheet5.Range("Targets") 'Blank

首先道歉,我是VBA新手

我想找到一种方法,我可以循环一行中的所有单元格,如果单元格是命名范围“目标”的一部分,我可以改变背景颜色

我最初是使用下面的代码来完成这项工作的,我会检查指定范围内的所有单元格,但速度太慢了,不实用。我希望通过将函数限制在活动行来加快宏的速度

我尝试过各种版本的Intersect,当我让它选择我想要查看的单元格时,我正在努力使用结果

Dim cell As Range
For Each cell In Sheet5.Range("Targets")
    'Blank cells
    If Cells(cell.Row, "JE").Value = "" Then
        cell.Interior.Color = xlNone
        cell.Font.Bold = False
        cell.Font.Color = vbBlack
    ElseIf cell.Value = "" And Month(Cells(cell.Row, "").Value) Mod 2 = 0 Then 'Odd
        cell.Interior.Color = RGB(221, 221, 221)
        cell.Font.Bold = False
        cell.Font.Color = vbBlack
    ElseIf cell.Value = "" And Month(Cells(cell.Row, "").Value) Mod 2 = 1 Then  'Even
        cell.Interior.Color = xlNone
        cell.Font.Bold = False
        cell.Font.Color = vbBlack
    '1-5 days Early (Green)
    ElseIf cell.Offset(0, 1).Value = "" And cell.Value >= Application.WorksheetFunction.WorkDay(Date, 1, [Support!B4:B100]) And cell.Value <= Application.WorksheetFunction.WorkDay(Date, 5, [Support!B4:B100]) Then
        cell.Interior.Color = RGB(188, 253, 175)
        cell.Font.Bold = True
        cell.Font.Color = RGB(84, 130, 53)
    '1-3 Days Overdue (Orange)
    ElseIf cell.Offset(0, 1).Value = "" And cell.Value <= Application.WorksheetFunction.WorkDay(Date, -1, [Support!B4:B100]) And cell.Value >= Application.WorksheetFunction.WorkDay(Date, -3, [Support!B4:B100]) Then
        cell.Interior.Color = RGB(255, 168, 39)
        cell.Font.Bold = True
        cell.Font.Color = vbWhite
    '1-3 Days Overdue (Red)
    ElseIf cell.Offset(0, 1).Value = "" And cell.Value < Application.WorksheetFunction.WorkDay(Date, -3, [Support!B4:B100]) Then
        cell.Interior.Color = RGB(158, 0, 0)
        cell.Font.Bold = True
        cell.Font.Color = vbWhite
    'Today (Blue)
    ElseIf cell.Offset(0, 1).Value = "" And cell.Value = Application.WorksheetFunction.WorkDay(Date, 0, [Support!B4:B100]) Then
        cell.Interior.Color = RGB(4, 119, 224)
        cell.Font.Bold = True
        cell.Font.Color = vbWhite
    ElseIf Month(Cells(cell.Row, "").Value) Mod 2 = 0 Then 'Odd
        cell.Interior.Color = RGB(221, 221, 221)
        cell.Font.Bold = False
        cell.Font.Color = vbBlack
    Else: Month (Cells(cell.Row, "").Value) Mod 2 = 1 'Even
        cell.Interior.Color = xlNone
        cell.Font.Bold = False
        cell.Font.Color = vbBlack
    End If
Next
Dim单元格作为范围
表5.范围(“目标”)中的每个单元格
“空白单元格
如果单元格(cell.Row,“JE”).Value=”“,则
cell.Interior.Color=xlNone
cell.Font.Bold=False
cell.Font.Color=vbBlack
ElseIf cell.Value=“”和Month(Cells(cell.Row,”).Value)Mod 2=0,然后是“奇数”
cell.Interior.Color=RGB(221221221221)
cell.Font.Bold=False
cell.Font.Color=vbBlack
ElseIf cell.Value=“”和Month(Cells(cell.Row,”).Value)Mod 2=1,然后为“偶数”
cell.Interior.Color=xlNone
cell.Font.Bold=False
cell.Font.Color=vbBlack
'提前1-5天(绿色)

ElseIf cell.Offset(0,1).Value=“”和cell.Value>=Application.WorksheetFunction.WorkDay(日期,1,[Support!B4:B100])和cell.Value检查一个范围是否在另一个范围内,您可以使用

下面是一个检查A1是否在NamedRange中的示例:

If Not Intersect(Range("A1"), Range("NamedRange")) Is Nothing Then
    'A1 is in NamedRange
Else
    'A1 is not it NamedRange
End If

谢谢,这让我走上了正轨。所有这些都像蒸汽火车一样固定和运行。