在do while循环中的下一个-无法完全运行 [英] for next inside a do while loop - not fully functioning
问题描述
我编写了一个do while循环,该循环很好用,但是当我尝试在其中的每个next循环中添加一个do时,它只会在第一个do while循环之后停止.我真的不确定我需要添加/删除什么才能回到正常运行的循环
I've written a do while-loop that works well, but when I try to add a for each-next loop inside it, it stops after only the first do while-loop. I'm really not sure what I need to add/remove to get back to a functioning loop
rgData,rgHeader和RowSum是我的代码前面定义的范围
rgData, rgHeader, and RowSum are ranges that are defined earlier in my code
Dim myCell As Range, c As Range, firstAddress As String
Const strFindMe As String = "Index"
With rgData
Set c = rgHeader.Find(what:=strFindMe, Lookat:=xlPart).Offset(1, 0)
If Not c Is Nothing Then
firstAddress = c.Address
Do
Dim ColIndex As Range
Set ColIndex = Range(c.Address, Cells(Range(c.Address).Offset(MktCt - 1, 0).Row, Range(c.Address).Column))
For Each myCell In ColIndex
myCell.FormulaR1C1 = "=IF(RC[-3]<>"""",RC[-3]/R" & RowSum & "C[-3]*100,"""")"
myCell.NumberFormat = "0"
Next
Set ColIndex = Nothing
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
我尝试以这种方式编写代码的原因是,因为我收到的报告已经被透视,因此,针对多个人口统计指标可能需要多个索引"列
the reason I'm attempting to write the code this way, is because the report I receive is already pivoted, and so there can be several "index" columns required for multiple demographic measures
当前,这适用于第一列索引"列,但不会移至下一个索引"列.
currently this is working for the first "index" column, but isn't moving to the next "index" column.
任何想法都会非常有帮助,谢谢
Any ideas would be very helpful, thanks
推荐答案
您的第一个查找针对 rgHeader
,但是您的 .FindNext
引用了 rgData
(通过With块)
Your first Find is against rgHeader
but your .FindNext
references rgData
(via the With block)
为简化逻辑,我将查找与处理分开:
To simplify the logic I would separate the Find from the processing:
Dim matches as Collection, m
Set matches = FindAll(rgHeader, strFindMe)
For Each m in matches
'process m
Next m
执行查找的单独功能:
Public Function FindAll(rng As Range, val As String) As Collection
Dim rv As New Collection, f As Range
Dim addr As String
Set f = rng.Find(what:=val, after:=rng.Cells(rng.Cells.Count), _
LookIn:=xlValues, LookAt:=xlPart)
If Not f Is Nothing Then addr = f.Address()
Do Until f Is Nothing
rv.Add f
Set f = rng.FindNext(after:=f)
If f.Address() = addr Then Exit Do
Loop
Set FindAll = rv
End Function
这篇关于在do while循环中的下一个-无法完全运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!