我在单独的工作表上有两个表,我正在遍历一个表,在另一个表上进行一些处理。
Dim shPoints As Worksheet
Dim shEmployees As Worksheet
Dim rngPoints As Range
Dim rngEmployee As Range
Dim strEUID as String ' Stores the Employee ID
Dim intRow as Integer ' Current row
Set shEmployees = ThisWorkbook.Sheets("Employees")
Set rngEmployee = shEmployees.Range("tblEmployees")
Set shPoints = ThisWorkbook.Sheets("Points")
Set rngPoints = shPoints.Range("tblPoints")
shEmployee.AutoFilterMode = False
shPoints.AutoFilterMode = False
' On Error Resume Next ' Works with this turned on
For intRow = 1 To shEmployee.Range("tblEmployees").Rows.Count
strEUID = shEmployees.Range("tblEmployees[EUID]").Cells(intRow)
rngPoints.AutoFilter Field:=1, Criteria1:=strEUID
' The filter above produces empty results occasionally because some employees do not have any points.
' Next line error "No cells were found"
If shPoints.Range("tblPoints").SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
' Do stuff
End If
Next intRow
有一个员工表,我将迭代进行一些计算,以跟踪员工在点系统跟踪中的发生情况。如果我输入“On Error Resume Next”,该代码适用于实际出现的员工。对于有清洁票证的员工,以下行错误地说“没有找到单元格”。我在网上找到了各种解决方案,包括一个on this site,但都没有。
编辑:我已经替换了一个解决方法但仍然真的想知道是否可以在没有On Error Resume Next的情况下处理这个“没有找到单元格”的错误。解决方法是使用countif函数在Occurences表中搜索Employees表中的Employees ID,如果计数为零则跳过它。
If shPoints.Application.CountIf(Range("tblPoints[EUID]"), strEUID) > 0 Then
'Do Stuff
End If
答案 0 :(得分:0)
的答案,如果这"没有找到细胞"错误可以在没有On Error Resume Next的情况下处理。是NO
这就是SpecialCells的工作原理。
一般表格是
Sub Demo()
Dim rFiltered As Range
'...
' Other code
'...
Set rFiltered = Nothing
On Error Resume Next
Set rFiltered = YourRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0 ' or use your own error handler
If Not rFiltered Is Nothing Then
' Process filtered list
End If
'...
' Other code
'...
End Sub