将项目添加到“使用...结束时”时出现Excel错误91

钙化

我在Do / With函数下添加一个项目时会抛出错误91。(感谢克里斯·尼尔森的代码)

Dim ws As Worksheet
Dim SrchRng As Range
Dim SearchValues() As Variant
Dim cl As Range, addr As String
Dim i As Long

SearchValues = Array(217, 317, 298)

Set ws = ActiveSheet
With ws
    Set SrchRng = Range(.Cells(1, 7), .Cells(.Rows.Count, 7).End(xlUp))
End With

For i = LBound(SearchValues) To UBound(SearchValues)

    Set cl = SrchRng.Find(SearchValues(i), LookIn:=xlValues)
    If Not cl Is Nothing Then
        addr = cl.Address
        Do
            With cl.EntireRow
                .Font.ColorIndex = 2
                .Interior.ColorIndex = 1
            End With
            Set cl = SrchRng.FindNext(cl)
        Loop While cl.Address <> addr
    End If
Next

变为以下内容时将引发错误:

Dim ws As Worksheet
Dim SrchRng As Range
Dim SearchValues() As Variant
Dim cl As Range, addr As String
Dim i As Long

SearchValues = Array(217, 317, 298)


Set ws = ActiveSheet
With ws
    Set SrchRng = Range(.Cells(1, 7), .Cells(.Rows.Count, 7).End(xlUp))
End With

For i = LBound(SearchValues) To UBound(SearchValues)

    Set cl = SrchRng.Find(SearchValues(i), LookIn:=xlValues)
    If Not cl Is Nothing Then
        addr = cl.Address
        Do
            With cl.EntireRow
                .Font.ColorIndex = 2
                .Interior.ColorIndex = 1
                .ClearContents
            End With
            Set cl = SrchRng.FindNext(cl)
        Loop While cl.Address <> addr
    End If
Next

唯一的增加是Do / With语句下的.ClearContents,除非我丢失了某些内容,否则似乎并没有为我增加一个变量。有人有什么想法吗?

**注意:它会执行预期的操作,只会引发错误。

布雷特杰

清除单元格时,cl可能Nothing需要删除循环之外的范围,或添加测试Nothing

方法1将更快

方法1-删除单拍范围

Sub A()
Dim ws As Worksheet
Dim SrchRng As Range
Dim SearchValues() As Variant
Dim cl As Range, addr As String
Dim i As Long
Dim rng2 As Range    

SearchValues = Array(217, 317, 298)

Set ws = ActiveSheet
With ws
    Set SrchRng = Range(.Cells(1, 7), .Cells(.Rows.Count, 7).End(xlUp))
End With    

For i = LBound(SearchValues) To UBound(SearchValues)
Set rng2 = Nothing
    Set cl = SrchRng.Find(SearchValues(i), LookIn:=xlValues)
    If Not cl Is Nothing Then
        addr = cl.Address
        Do
        If Not rng2 Is Nothing Then
            Set rng2 = cl.EntireRow
        Else
            Set rng2 = Union(rng2, cl.EntireRow)
        End If
        Set cl = SrchRng.FindNext(cl)
        Loop While Not cl Is Nothing
    End If
    If Not rng2 Is Nothing Then
        With rng2
            .Font.ColorIndex = 2
            .Interior.ColorIndex = 1
            .ClearContents
        End With
    End If
Next

End Sub

方法2

With cl.EntireRow
            .Font.ColorIndex = 2
            .Interior.ColorIndex = 1
            .ClearContents
End With
    Set cl = SrchRng.FindNext(cl)
Loop While Not cl is Nothing  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将AFNetworking添加到新项目时出现错误

使用ARC将C库添加到项目时出现错误

将项目添加到列表时出现AccessViolationException

将项目添加到GoogleMap时,Android AsyncTask错误

JPQL/HQL 错误:使用 LEFT JOIN 输入结束时出现语法错误

错误:尝试将 sql 查询的结果保存到 json 时,输入错误结束时出现语法错误

使用 Cocoapods 添加到项目时发生 Firebase 错误

将项目添加到xml文件时出现奇怪的aapt.exe错误

EF - 将项目添加到虚拟 ICollection 后保存时出现 InvalidOperationException 错误

将AdMob插件添加到离子项目时出现Xcode错误

将Firebase添加到我的Android项目时出现错误

尝试将文件从库添加到主项目时出现错误?

使用apache poi将项目符号添加到word文档时出错

使用排序的 QAbstractTableModel 时如何将“选择项目...”添加到 QComboBox?

不使用 XAML 时如何将 UWP 项目添加到 Xamarin 解决方案

Ruby on Rails:尝试将项目添加到购物车时出错(使用订单项)

使用Swashbuckle时将全局项目添加到swagger.json

使用appcompat-v7:28.0.0将Firebase Analytics添加到Android项目时出错

没有ID时使用JS将项目添加到html列表

System.NullReferenceException:将项目添加到列表时

单击按钮时将项目添加到RecyclerView

将项目添加到ObservableCollection时发生WPF事件

Java在迭代时将项目添加到ArrayList

recyclerview 在将新项目添加到列表时刷新

将AdMob添加到Firebase项目时崩溃

将项目添加到数组时视图不会更新

将iOS平台添加到phonegap项目时出错

将项目添加到ObservableCollection时,InteropServices.COMException

尝试将 admob 添加到项目时出错