首页 > 解决方案 > 有效的单元格引用停止运行

问题描述

我有一个正在运行的宏,它在使用范围引用的一行之后突然抛出错误“1004”,即使这个引用在这一点上工作得很好。请参阅下面的代码片段示例。

Option Explicit

Dim ws1 as Worksheet
Set ws1 = ThisWorkbook.Worksheets("Overview")
Dim PasteRange as Range
Set PasteRange = ws1.Range("AJ4:AM4") 'This is actually a variable range, but this is not important for this question
Dim CheckCell as Range
Dim ColCntr as Long
Dim ReviewCntr as Long
Dim LoopCntr as Long
ReviewCntr = ws1.Range("AA1").Value 'This cell only contains numerical values

For Each CheckCell in PasteRange
    If LoopCntr = 0 Then
        'A lot of code happens here, referencing CheckCell, all working 
        'flawlessly. Untill...
        ColCntr = CheckCell.Column + ReviewCntr 'Final good working line
    ElseIf LoopCntr <> 0 Then
        CheckCell.Offset(0, BackSet2).Interior.ColorIndex = 3 '<-- error occurs
        'Some More Code
    End If
    LoopCntr = LoopCntr + 1
Next CheckCell

突然上线出现对象定义错误

CheckCell.Offset(0, BackSet2).Interior.ColorIndex = 3

而且我不知道为什么或如何突然发生此错误,特别是因为它上面的行,引用仍然有效。通过 Debug DropDown 菜单编译项目不会产生错误。在单步执行代码时将鼠标悬停在Checkcell上面时,它是有效的,并在弹出窗口中给出值和不给出的值。

代码到达特定行的那一刻,弹出窗口显示:

应用程序定义或对象定义的错误。

悬停BackSet2在该行上会读取一个数值,这意味着偏移量正常工作。将鼠标悬停在CheckCell位于此行之前的引用上仍会在弹出窗口中产生相关信息。

谁能告诉我这里的错误是什么?

标签: excelvba

解决方案


推荐阅读