1

以下代码在粗体线上给了我一个应用程序定义或对象定义的错误。我设定范围;但是,显然excel无法读取它。我不确定为什么会出现该错误。任何帮助深表感谢!谢谢。

Public Sub specialattention()
'Loop to check whether issue needs spcial attention
Dim n As Variant
Dim nend As Range
Dim nrng As Range
Dim nnow As Date
Dim ninterval As Integer
ninterval = Sheets("Ranges").Range("AB3").Value
nnow = Sheets("Issues Database").Range("N5").Value
Set nend = Sheets("Issues Database").Range("M1048576").End(xlUp)
Set nrng = Sheets("Issues Database").Range("M6", nend)
For Each n In nrng
    If n.Value = "1" And nnow - n.Offset(0, 1).Value >= ninterval Then
        n.Offset(0, 2).Value = "1"
    Else
        n.Offset(0, 2).Value = "0"
    End If
    'Color Cells that need special attention
    If n.Offset(0, 2).Value = "1" Then
            Dim xrng1 As Range
            Dim xrng2 As Range
            Set xrng1 = n.Offset(0, -11)
            Set xrng2 = n.Offset(0, -1)
            **Sheets("Issues Database").Range(xrng1, xrng2).Interior.ColorIndex = 19**
        Else
            Dim xrng3 As Range
            Dim xrng4 As Range
            Set xrng3 = n.Offset(0, -11)
            Set xrng4 = n.Offset(0, -1)
            **Sheets("Issues Database").Range(xrng2, xrng3).Interior.ColorIndex = 2**
        End If
    Next
    End Sub
4

1 回答 1

0

只要传递给 .Range 的范围是有效范围,您的代码就应该可以工作。

根据您的代码,第一个实例:

Sheets("Issues Database").Range(xrng1, xrng2).Interior.ColorIndex = 19 应该没事。

但是,您的第二个实例:

Sheets("Issues Database").Range(xrng2, xrng3).Interior.ColorIndex = 2正在引用不在范围内的范围,因为您正在重用xrng2在不同逻辑路径中定义的范围。

可以将您的第二个问题行更新为此:

Sheets("Issues Database").Range(xrng3, xrng4).Interior.ColorIndex = 2会给你你预期的行为。

于 2012-12-11T22:20:38.173 回答