【问题标题】:Exclude multiple cells from range从范围中排除多个单元格
【发布时间】:2020-02-20 16:57:31
【问题描述】:

我的问题是如何从范围对象中删除一个或多个单元格?我之前问过一些非常相似的问题,有人向我指出了这个问题:Remove cell from Range (object)

接受的答案:

Function getExcluded(ByVal rngMain As Range, rngExc As Range) As Range
    Dim rngTemp     As Range
    Dim rng         As Range

    Set rngTemp = rngMain
    Set rngMain = Nothing

    For Each rng In rngTemp
        If rng.Address <> rngExc.Address Then
            If rngMain Is Nothing Then
                Set rngMain = rng
            Else
                Set rngMain = Union(rngMain, rng)
            End If
        End If
    Next

    Set getExcluded = rngMain
End Function


Sub test()
    MsgBox getExcluded(Range("A1:M10000"), Range("a10")).Address
End Sub

仅当排除范围是单个单元格时,接受的答案才有效 - 至少当我尝试它时对我来说是这样。我要排除的单元格通常不止一个单元格,因此我尝试调整代码:

我的尝试:

Function getExcluded(ByVal rngMain As Range, rngExcl As Range) As Range
    Dim rngTemp As Range
    Dim cellTemp As Range, cellExcl As Range

    Set rngTemp = rngMain
    Set rngMain = Nothing

    For Each cellTemp In rngTemp 'go through all cells in established range
        If Intersect(cellTemp, rngExcl) Is Nothing Then 'check for each cell if it intersects with the range to be excluded; no overlap -> put it into rngMain
            If rngMain Is Nothing Then
                Set rngMain = cellTemp
            Else
                rngMain = Union(rngMain, cellTemp)
            End If

            Debug.Print "cellTemp: " & cellTemp.Address
            Debug.Print "rngMain: " & rngMain.Address

        End If
    Next cellTemp

    Set getExcluded = rngMain


Sub test5()

    getExcluded(Range("A1:D3"), Range("B1:C1")).Select
End Sub

问题似乎出现在Set rngMain = Union(rngMain, rng) 行中。我的Debug.Print 语句告诉我cellTemp 正在按应有的方式进行迭代;然而,即使带有Union 的行被执行并且无论cellTemp 是什么,rngMain 仍然保持$A$1

我做错了什么?

【问题讨论】:

  • @FaneDuru 新问题,与上一个问题有点不同。如果您的代码 sn-p 适用,我会知道,但如果适用,我会很感兴趣
  • 您缺少set。想知道为什么它不会出错...
  • rngMain = Union(rngMain, cellTemp)
  • @SJR 所以代码很好,除了缺少set?我的意思是它现在正在工作,谢谢你!很好奇我没有收到错误。

标签: excel vba range


【解决方案1】:

以@Nathan_Sav 为基础。

这将允许添加许多排除范围:

Function testexclude(rngMain As Range, ParamArray rngExclude() As Variant) As Range


Dim i As Long
For i = LBound(rngExclude, 1) To UBound(rngExclude, 1)
    Dim rngexcluderng As Range
    If rngexcluderng Is Nothing Then
        Set rngexcluderng = rngExclude(i)
    Else
        Set rngexcluderng = Union(rngexcluderng, rngExclude(i))
    End If
Next i


Dim c As Range
For Each c In rngMain

    If Intersect(c, rngexcluderng) Is Nothing Then
        Dim r As Range
        If r Is Nothing Then
            Set r = c
        Else
            Set r = Union(r, c)
        End If
    End If

Next c

Set testexclude = r

End Function

【讨论】:

    【解决方案2】:

    类似这样,也设置联合范围

    Function testexclude(rngMain As Excel.Range, rngExclude As Excel.Range) As Excel.Range
    
    Dim c As Excel.Range
    Dim r As Excel.Range
    
    For Each c In rngMain
    
        If Intersect(c, rngExclude) Is Nothing Then
            If r Is Nothing Then
                Set r = c
            Else
                Set r = Union(r, c)
            End If
        End If
    
    Next c
    
    Set testexclude = r
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多