我有类似下面的代码,我想检查工作表中是否满足命名范围的组,如果是的话做了什么,但是如果在行中没有满足范围我收到错误,如何解决?谢谢
If Range("Range1") Is Nothing Or Range("Range2") Is Nothing Or Range("Range3") Is Nothing Or Range("Range4") Is Nothing Or Range("Range5") Is Nothing Then
MsgBox "Check if in file each of required range is mentioned!"
Else
'do something
End if
答案 0 :(得分:0)
其中一种方法是检查.Name
是否与您定义的Range
匹配,并使用Integer
计数器匹配是否找到Ranges
的数量
请参阅以下代码:
Private Sub CheckNamedRanges()
Dim nm As Name
Dim wb As Workbook
Dim i As Integer
Set wb = ActiveWorkbook
For Each nm In wb.Names
If nm.Name = "Range1" Or nm.Name = "Range2" Or nm.Name = "Range3" Or nm.Name = "Range4" Or nm.Name = "Range5" Then
i = i + 1
End If
Next
If i = 5 Then
'do something
Else
MsgBox "Check if in file each of required range is mentioned!"
End If
End Sub
(请注意我并非100%确定这是最有效的方式)
答案 1 :(得分:0)
尝试这样的事情
Sub CheckTheRange()
Dim CheckMyRange As Range
On Error Resume Next
Set CheckMyRange = Range("MyRange")
On Error GoTo 0
If CheckMyRange Is Nothing Then
'Error, range not found
Else
'Go for it
End If
End Sub