我有一个名为的单元格名称,如果单元格在范围内( ) Book.a
,我必须检查条件。如何做到这一点?我为此尝试了“相交”方法,但对我不起作用。请提出一些答案。if
B1:I10
问问题
186 次
2 回答
1
这是你正在尝试的吗?
Option Explicit
Sub Sample()
Dim rng As Range
With Sheets("Sheet1")
On Error Resume Next
Set rng = Application.Intersect(.Range("Book.a"), .Range("B1:I10"))
On Error GoTo 0
If Not rng Is Nothing Then
MsgBox "Range `Book.a` is a part of `B1:I10`"
Else
MsgBox "Range `Book.a` not found or is not a part of `B1:I10`"
End If
End With
End Sub
于 2013-01-21T11:43:57.373 回答
0
在一个范围内搜索命名范围似乎是不可能的。所以我收回我的评论。您可以尝试的是:对于每个类似的命名范围book.a
,您可以检查其地址是否intersect
为给定的主范围..
Dim objName As Name
Dim mainRange as Range
Sheet1.Unprotect
Set mainRange = ActiveWorkbook.Sheets(1).Range("B1:I10")
For Each objName In ActiveWorkbook.Names
strName = objName.Name
If InStr(1, strName, "book.a", vbTextCompare) > 0 Then
If Intersect(objName.Address, mainRange) Is Nothing then
'-- not within
Else
'-- within
End If
End If
Next
Sheet1.Protect
于 2013-01-21T11:36:01.467 回答