我需要编写通用宏以对不同表中的条件条件列进行计数。
问题是每个表可以包含不同数量的列。
因此,如果所需的列不存在,则会出现错误。
在这种情况下,我想应用错误处理程序,该处理程序将写在“ null”列中。
我尝试下面的代码,它可以正常工作,错误不会出现并且宏继续运行,但是列中的单元格仍然为空,没有“ null” 问题是什么?也许有更好的方法来管理它?
Function Section800_error()
Dim zelle As Range
Dim i As Integer
Dim posMonitoring As Integer
Dim j As Integer
Dim intLastRow As Integer
On Error GoTo Handler
With Sheets("ICS Table")
Set zelle = .Cells.Find("800_Section", lookat:=xlPart)
posMonitoring = zelle.Column
intLastRow = .UsedRange.Rows.Count
For i = 2 To intLastRow
If .Cells(i, posMonitoring).Value < 1 Or .Cells(i, posMonitoring).Value > 10 Then
Sheets("Section_errors").Cells(i, 8) = "err700"
Else
Sheets("Section_errors").Cells(i, 8) = "no"
End If
Next i
End With
Exit Function
Handler:
For i = 2 To intLastRow
Sheets("Section_errors").Cells(i, 8).Value = "null"
Next i
End Function
答案 0 :(得分:0)
可能的更正
Function Section800_error()
Dim zelle As Range
Dim i As long
Dim posMonitoring As Integer
Dim intLastRow As Integer
With Sheets("ICS Table")
Set zelle = .Cells.Find("800_Section", lookat:=xlPart)
If zelle Is Nothing Then
For i = 2 To intLastRow
Sheets("Section_errors").Cells(i, 8).Value = "null"
Next i
Else
posMonitoring = zelle.Column
intLastRow = .UsedRange.Rows.Count
For i = 2 To intLastRow
If .Cells(i, posMonitoring).Value < 1 Or .Cells(i, posMonitoring).Value > 10 Then
Sheets("Section_errors").Cells(i, 8) = "err700"
Else
Sheets("Section_errors").Cells(i, 8) = "no"
End If
Next i
End If
End With
End Function