excel - 从范围(对象)中删除单元格

标签 excel excel-2013 vba

背景
我的代码在范围内执行一些循环,但是,每个交互都应该在不包括刚刚执行的单元格的范围内执行。我认为更简单的方法是将单元格从存储范围中删除。
问题
我无法找到从存储的对象中删除单元格的方法
代码
这个问题很普遍,但是,对于事情来说,它会是这样的

Sub Sample()
Dim RangeToAnalyze As Range
Dim CounterRange As Long
Dim ExcludeCell As Range 'sample on what is desired to achieve
    Set RangeToAnalyze = Selection 'this is based on some other criteria but, in order to reproduce it easier that's why selection
    For CounterRange = 1 To 5
    Set ExcludeCell = RangeToAnalyze.Find("text")
    'now here I would like to find the next cell, but it should exclude the first one in order to go to the next one
    Set RangeToAnalyze = RangeToAnalyze.Exclude(ExcludeCell) 'this is what I want to do, so when looping it could jump to the next find (This function is "sample" this is what I am looking to do
    Next CounterRange
End Sub

最佳答案

一种方法可能是这样

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

关于excel - 从范围(对象)中删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39103551/

相关文章:

excel - 在 Excel 中,是否有一种有效的方法来总结重叠的命名范围?

excel - 将 Excel 图表复制到 Word 的 VBA 脚本在更高版本的 Word 中不起作用

excel - Excel VBA 的 Rnd() 真的有这么糟糕吗?

excel - 如何将 MS Excel 连接到 Azure Cosmos DB-SQLAPI?

excel - 粘贴数据后取消选择列

vba - 根据单元格中的值对单元格进行颜色

vba - 使用 VBA 将文本转换为 Excel 中的列

vba - 循环遍历列表并将单元格复制到右侧

vb.net - 将 Excel 文件附加到完成的 Visual Basic 项目

excel - VBA 找不到我的工作表名称