【发布时间】:2014-06-20 00:56:03
【问题描述】:
当我在 Do/With 函数下添加项目时,我有一段代码会引发错误 91。 (感谢 chris neilsen 提供代码)
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,除非我遗漏了什么,否则它似乎没有添加我所知道的变量。有人有什么想法吗?
**注意:它做了它应该做的,它只是抛出一个错误。
【问题讨论】:
标签: excel with-statement vba