vba - 运行时错误 91 - 对象变量或未设置 block 变量

标签 vba excel

有人可以指出我的代码有什么问题,因为我得到了...

Run-time error 91 - Object variable or With block variable not set



在下面代码的这一行

Loop While c2.Address <> e


Dim Cell As Range
Dim SrchRng2 As Range
Dim c2 As Range, e As String

'Check each row in column - if BLUE text (set by CF) change to #N/A
For Each Cell In Intersect(Columns("E"), ActiveSheet.UsedRange)
    If Cell.DisplayFormat.Font.ColorIndex = 5 Then Cell.Value = "#N/A"
Next
On Error GoTo NoBlueText
'Search column E for cells with #N/A and clear cells across columns E:G in row
Set SrchRng2 = ActiveSheet.Range("E2", ActiveSheet.Range("E" & Rows.Count).End(xlUp))
Set c2 = SrchRng2.Find("#N/A", LookIn:=xlValues)

If Not c2 Is Nothing Then
    e = c2.Address
Do
            ActiveSheet.Range("E" & c2.Row & ":G" & c2.Row).Cells.ClearContents
        Set c2 = SrchRng2.FindNext(c2)
    Loop While c2.Address <> e
End If

NoBlueText:

最佳答案

既然您首先在单元格中放置“#N/A”,然后再查找它们,那么在第一阶段直接采取行动不是更简单吗?

With ActiveSheet
    With .Range("E2", .Cells(.Rows.count, "E").End(xlUp))
        For Each cell In .Cells
            If cell.DisplayFormat.Font.ColorIndex = 5 Then cell.Resize(, 3).ClearContents
        Next
    End With
End With

关于vba - 运行时错误 91 - 对象变量或未设置 block 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398934/

相关文章:

excel - 无论条目的大小写如何,都防止列中出现重复

c# - 从 C# Access VBA 函数

vba - 从第二个宏中卸载用户窗体

excel - VBE 的快捷方式在 Excel 2016 中不起作用

VBA - 内存优化的类与类型

excel - VBA 启用编辑

excel - 获取文本框的值到单元格

mysql - 创建返回 Mysql 数据的自定义 Excel 函数

arrays - 如何使用 VBA 在 JSON 文件中获取多个数组中的所有项目?

excel - Excel VBA错误处理程序找不到文件…如何跳过它?