Excel 从范围(对象)中删除单元格
背景Excel 从范围(对象)中删除单元格,excel,excel-2013,vba,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
我的代码在范围内执行一些循环,但是,每个交互都应该在不包括刚刚执行的单元格的范围内执行。我认为更简单的方法是从存储范围中删除单元格。
问题
我无法找到从存储对象中删除单元格的方法
代码
这个问题是一般性的,但就问题而言,可能是这样的
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
您最好使用一个用于。。。我怀疑的每一个循环。这应该是一个起点:
Sub Sample()
Dim RangeToAnalyze As Range
Dim rngCell as Range
Set RangeToAnalyze = Range("Selection")
For each rngCell in RangeToAnalyze
'Your other code/actions here
Next rngCell
'more code here
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
要查找第五个“文本”
,可以使用.FindNext
Set ExcludeCell = RangeToAnalyze.Find("text")
Dim CounterRange As Long
For CounterRange = 1 To 5
If Not ExcludeCell Is Nothing Then
Select Case CounterRange
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
End Select
End If
Set ExcludeCell = RangeToAnalyze.FindNext
Next CounterRange
'If Not ExcludeCell Is Nothing And CounterRange = 5 Then MsgBox ExcludeCell.Address
另一种选择是临时用其他内容替换找到的“text”
For CounterRange = 1 To 5
Set ExcludeCell = RangeToAnalyze.Find("text")
If Not ExcludeCell Is Nothing Then
Select Case CounterRange
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
End Select
End If
Next CounterRange
' use RangeToAnalyze
RangeToAnalyze.Replace "not text", "text"
替代方法是使用
Union
将5个“text”
范围存储到一个范围中,清除该范围的值,然后在完成后将其设置回“text”
。据我所知,没有实现此功能的本机方法。不过你有几个选择。您可以创建一个UDF,该UDF将从一个区域中删除一个单元格,您可以改变定义范围的方式,或者您可以像正常情况一样在该区域上循环并检查“文本”,如果该单元格包含文本,则不在该单元格上运行代码,循环将跳到下一个单元格。我个人推荐最后一个选项,它需要对当前代码进行最少的调整,并且运行速度仍然很快。我想UDF是一种方法,你刚才给了我一个如何做的想法。遗憾的是,没有一种方法可以以本地方式实现这一点。你不会喜欢这个答案,但你必须做大量的相交
或联合
。你想找到第五个“文本”
?看这个:我想这是一个解决问题的方法,但是,我需要控制每个的循环。我可以添加一个计数器,但如果是这样的话,我会回到aFor/to
logic@Sgdva您需要以何种方式控制循环?您可以在for之后添加if语句。。。每个都要移动到代码的另一部分或退出循环。逻辑是一个通过“查找”的循环,而不是每个,代码将花费很长时间分析每个单元格,而不是执行查找,根据它的出现情况,会触发另一个事件(如果找到的文本是第5个,则..)example@Sgdva那么,您是否正在尝试查找文本字符串“5th”在单元格值内?不,我在循环,对于每个交互,我做一些不同的事情,例如,如果找到1,那么x=2,如果找到2,那么y=x/3谢谢!但是,这是一个包含所有元素的循环,我必须为1,2,3,n做这个逻辑。。。这就是为什么我需要一些一般性的东西来根据需要的情况来调用。这是我第一次评论时正在做的事情,是的,这就是我要找的!对于进一步的引用,我刚刚发现,如果解析为rngMain的参数是一整列/整行,那么这将不起作用,它应该被限制在调用它的函数中on@Sgdva将如果rng.地址rngExc.地址更改为如果Intersect(rng,rngExc)为空,则更改为
For CounterRange = 1 To 5
Set ExcludeCell = RangeToAnalyze.Find("text")
If Not ExcludeCell Is Nothing Then
Select Case CounterRange
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
End Select
End If
Next CounterRange
' use RangeToAnalyze
RangeToAnalyze.Replace "not text", "text"