Excel 对于do while循环内的下一步-未完全运行

Excel 对于do while循环内的下一步-未完全运行,excel,vba,Excel,Vba,我已经编写了一个do-while循环,它工作得很好,但是当我尝试为其中的下一个循环添加一个for时,它只在第一个do-while循环之后停止。我真的不确定我需要添加/删除什么才能回到正常运行的循环 rgData、rgHeader和RowSum是前面在代码中定义的范围 Dim myCell As Range, c As Range, firstAddress As String Const strFindMe As String = "Index" With rgData

我已经编写了一个do-while循环,它工作得很好,但是当我尝试为其中的下一个循环添加一个for时,它只在第一个do-while循环之后停止。我真的不确定我需要添加/删除什么才能回到正常运行的循环

rgData、rgHeader和RowSum是前面在代码中定义的范围

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
Dim myCell作为范围,c作为范围,firstAddress作为字符串
Const strFindMe As String=“Index”
使用rgData
设置c=rgHeader.Find(what:=strFindMe,Lookat:=xlPart).偏移量(1,0)
如果不是,那么c什么都不是
firstAddress=c.地址
做
Dim ColIndex As范围
设置ColIndex=Range(c.Address,单元格(Range(c.Address).偏移量(MktCt-1,0).行,范围(c.Address).列))
对于ColIndex中的每个myCell
myCell.FormulaR1C1=“=IF(RC[-3]”,RC[-3]/R“&RowSum&“C[-3]*100.””
myCell.NumberFormat=“0”
下一个
设置ColIndex=无
集合c=.FindNext(c)
循环而不是c为Nothing,c.Address为firstAddress
如果结束
以
我之所以尝试以这种方式编写代码,是因为我收到的报告已经是数据透视的,因此对于多个人口统计指标,可能需要几个“索引”列

目前,这适用于第一个“索引”列,但不会移动到下一个“索引”列


任何想法都会非常有用,谢谢

您的第一个发现是针对
rgHeader
的,但是您的
.FindNext
引用了
rgData
(通过With块)

为了简化逻辑,我将查找与处理分开:

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

您的第一次查找是针对
rgHeader
,但您的
.FindNext
引用了
rgData
(通过With块)

为了简化逻辑,我将查找与处理分开:

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

当您
Set c=.FindNext(c)
时,您之所以说
Set c=rgData.FindNext(c)
,是因为您的
With
语句。根据您最初的
rgHeader.Find
,它不应该是
rgHeader.FindNext(c)
?好眼力:我尝试用两种方式更改它(rgHeader在顶部,v rgHeader.Find),但两种方法都给出了相同的结果:只计算第一个“index”列。当
设置c=.FindNext(c)
时,您之所以说
Set c=rgData.FindNext(c)
,是因为您的
With
语句。根据您最初的
rgHeader.Find
,它不应该是
rgHeader.FindNext(c)
?好眼力:我试着用两种方法更改它(rgHeader在顶部,v rgHeader.Find),但两种方法都给出了相同的结果:只计算第一个“index”列。我喜欢这个想法,我可能需要花一点时间将其折叠到代码中并进行测试。我将很快更新我的进度。谢谢你的耐心。我输入了你的代码,工作起来很有魅力!非常感谢!我可能会重新编写一些早期的代码,以利用公共函数/集合概念,使其他一些部分更干净。我喜欢这个想法,可能需要一些时间将其折叠到代码中并进行测试。我将很快更新我的进度。谢谢你的耐心。我输入了你的代码,工作起来很有魅力!非常感谢!我可能会重新编写一些早期的代码,以利用公共函数/集合的概念,使其他一些部分也更干净