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