excel 如何使用XML查找一个范围是否包含在另一个范围中?

bhmjp9jg  于 2023-11-20  发布在  其他
关注(0)|答案(8)|浏览(156)

我有一个问题比较两个范围。为了简单起见,我将采取两个简单的范围M6:M10M6:M8,我想知道第二个是否包含在第一个和第一件事,我认为是写

  1. Sub example()
  2. Dim range1, range2, inte As range
  3. Set range1 = range("M6:M10")
  4. Set range2 = range("M6:M8")
  5. Set intersec = Intersect(range1, range2)
  6. If intersec = range2 Then
  7. [if statement]
  8. End If
  9. End Sub

字符串
但此过程返回以下错误:

  • PRB: Error 13 (Type Mismatch) & Error 3061 w/ SQL Queries*

所以也许我不能用这种方式使用方法“intersect”.

piztneat

piztneat1#

这里有一个方法:

  1. Sub ProperSubSet()
  2. Dim range1 As Range, range2 As Range, inte As Range
  3. Dim r As Range
  4. Set range1 = Range("M6:M10")
  5. Set range2 = Range("M6:M8")
  6. For Each r In range2
  7. If Intersect(r, range1) Is Nothing Then
  8. MsgBox "range2 is not a proper subset of range1"
  9. Exit Sub
  10. End If
  11. Next r
  12. MsgBox "range2 is a proper subset of range1"
  13. End Sub

字符串

展开查看全部
pbossiut

pbossiut2#

首先,将range1和range2变量声明为范围。
然后,当您将intersec变量与range2变量进行比较时,使用range方法的address属性来比较内容。
比如说:

  1. Sub example()
  2. Dim range1 As Range, range2 As Range, intersec As Range
  3. Set range1 = Range("M6:M10")
  4. Set range2 = Range("M11:M12")
  5. Set intersec = Intersect(range1, range2)
  6. If Not intersec Is Nothing Then
  7. If intersec.Address = range2.Address Then
  8. '[CODE GOES HERE]
  9. End If
  10. End If
  11. End Sub

字符串

展开查看全部
iqjalb3h

iqjalb3h3#

这里有一些你可以尝试的东西:

  1. Sub Test()
  2. Dim R1 As Range, R2 As Range, R3 As Range
  3. Set R1 = Application.InputBox("Select first range", , , , , , , 8)
  4. Set R2 = Application.InputBox("Select second range", , , , , , , 8)
  5. Set R3 = Intersect(R1, R2)
  6. If Not R3 Is Nothing Then
  7. If R3.Address = R1.Address Then
  8. MsgBox "First Range is subset of second"
  9. ElseIf R3.Address = R2.Address Then
  10. MsgBox "Second Range is subset of first"
  11. Else
  12. MsgBox "Neither range contained in the other"
  13. End If
  14. Else
  15. MsgBox "Ranges are disjoint"
  16. End If
  17. End Sub

字符串

展开查看全部
zzwlnbp8

zzwlnbp84#

我使用它的方式是这样的:

  1. If Application.Intersect(rng1, rng2) Is Nothing Then
  2. 'herecomesthecode
  3. Else
  4. 'herecomesthecode
  5. End if

字符串
你可以删除else或者写Not nothing,这取决于你想要实现什么。

crcmnpdw

crcmnpdw5#

另一种变体:

  1. Sub ProperSubSet2()
  2. Dim range1 As Range, range2 As Range
  3. Set range1 = [M6:M10]
  4. Set range2 = [M6:M8]
  5. Set rComp = Intersect(range2, range1)
  6. If Not rComp Is Nothing Then
  7. If rComp.Cells.Count = range2.Cells.Count Then
  8. MsgBox "range2 is a proper subset of range1"
  9. Else
  10. MsgBox "range2 is not a proper subset of range1"
  11. End If
  12. Else
  13. MsgBox "Both ranges aren't intersected at all!"
  14. End If
  15. End Sub

字符串

展开查看全部
muk1a3rh

muk1a3rh6#

你可以做一个交集与范围的比较,以确定一个范围是否包含在另一个范围内。一些代码来显示这一点。

  1. Sub TestExample()
  2. Dim Range1 As Range: Set Range1 = Range("M6:M10")
  3. Dim Range2 As Range: Set Range2 = Range("M6:M8")
  4. MsgBox Example(Range1, Range2)
  5. End Sub
  6. Function Example(Range1 As Range, Range2 As Range) As Integer
  7. Dim Overlay As Range: Set Overlay = Application.Intersect(Range1, Range2)
  8. If Not Overlay Is Nothing Then
  9. If Overlay.Address = Range1.Address Then Example = Example + 1
  10. If Overlay.Address = Range2.Address Then Example = Example + 2
  11. End If
  12. End Function

字符串
如果没有一个范围完全包含在另一个范围内,则该函数将返回0,如果第一个范围包含在第二个范围内,则返回1,如果第二个范围包含在第一个范围内,则返回2,如果范围相等,则返回3

展开查看全部
jtjikinw

jtjikinw7#

对于一个更强大的解决方案,适用于具有多个区域的范围,彼此不同的范围,具有非常大数量的单元格的范围(所以.CountLarge,而不是.Count),那么这将起作用:

  1. Function RangeContainsRange(BigRange As Range, SmallRange As Range) As Boolean
  2. If BigRange.Parent Is SmallRange.Parent Then
  3. RangeContainsRange = Application.Union(BigRange, SmallRange).Cells.CountLarge = BigRange.Cells.CountLarge
  4. Else
  5. RangeContainsRange = False
  6. End If
  7. End Function

字符串

3df52oht

3df52oht8#

  1. Function is_subrange(rn1 As Range, rn2 As Range) As Boolean
  2. Dim rn As Range
  3. is_subrange = True
  4. For Each rn In rn1.Cells
  5. If Intersect(rn, rn2) Is Nothing Then
  6. is_subrange = False
  7. Exit For
  8. End If
  9. Next
  10. End Function

字符串

相关问题