Vba 使用两个FindNext时出错

Vba 使用两个FindNext时出错,vba,Vba,此代码有两个范围。find/findnext用于在父/子值列表中搜索值和相关值 为父值创建一个包含唯一值的字典,然后使用字典作为父值在两个循环中使用范围查找 Sub tablaplana() Call SET_VAR '# FIND UNIQUE REGION CENTERS ' Dim region As Range ' Dim district As Range Set dict = CreateObject("Scripting.Dictionary") With

此代码有两个范围。find/findnext用于在父/子值列表中搜索值和相关值

为父值创建一个包含唯一值的字典,然后使用字典作为父值在两个循环中使用范围查找

Sub tablaplana()
  Call SET_VAR
  '# FIND UNIQUE REGION CENTERS
  ' Dim region As Range
  ' Dim district As Range
  Set dict = CreateObject("Scripting.Dictionary")
  With ws(3)
      r = 2
    x = 2
    Do Until x >= .Range("A1").End(xlDown).Row
        jnum = .Cells(x, 3)
        jtype = .Cells(x, 4)
        jname = .Cells(x, 6)
        If jtype = "R" And InStr(1, jname, "REGION ADMIN") Then
            If Not dict.Exists(jnum) Then
                dict.Add jnum, jname
                Debug.Print x
            End If
        End If
        x = x + 1
    Loop
    '# FIND ALL SUB-SUB-VALUES VALUES OF PARENT UNIQUE VALUES
    For Each k In dict.Keys
        s = 0
        Set region = .Range("C:C").Find(k, LookAt:=xlWhole)
        If Not region Is Nothing Then
            regionfirst = region.Address
            Do
                Set district = .Range("C:C").Find(.Cells(region.Row, 1), LookAt:=xlWhole)
                If Not district Is Nothing Then
                    districtfirst = district.Address
                    Do
                        '# ADD VALUES TO REPORT SHEET
                        ws(1).Cells(r, 1) = .Cells(district.Row, 1) 'sub-sub number
                        ws(1).Cells(r, 2) = .Cells(region.Row, 6) 'parent name
                        ws(1).Cells(r, 3) = .Cells(region.Row, 3) 'parent number
                        ws(1).Cells(r, 4) = .Cells(district.Row, 6) 'sub name
                        ws(1).Cells(r, 5) = .Cells(district.Row, 3) 'sub number
                        ws(1).Cells(r, 6) = .Cells(district.Row, 5) 'sub-sub name
                        Set district = .Range("C:C").FindNext(district)
                        r = r + 1
                    Loop While Not district Is Nothing And district.Address <> districtfirst
                End If
                Set region = .Range("C:C").FindNext(region)
            Loop While Not region Is Nothing And region.Address <> regionfirst
        End If
      Next k
  End With
End Sub
Sub-tablaplana()
调用集
“#寻找独特的区域中心
'暗区域作为范围
"暗区为射程"
Set dict=CreateObject(“Scripting.Dictionary”)
带ws(3)
r=2
x=2
直到x>=.范围(“A1”).结束(xlDown).行
jnum=.Cells(x,3)
jtype=.Cells(x,4)
jname=.Cells(x,6)
如果jtype=“R”和InStr(1,jname,“区域管理员”),则
如果不存在dict.Exists(jnum),则
添加jnum,jname
调试。打印x
如果结束
如果结束
x=x+1
环
“#查找父唯一值的所有子值
每k个数字键
s=0
设置区域=.Range(“C:C”).Find(k,LookAt:=xlother)
如果不是的话,区域就什么都不是了
regionfirst=区域地址
做
设置区域=.Range(“C:C”).Find(.Cells(region.Row,1),LookAt:=xlother)
如果不是,那就什么都不是了
districtfirst=地区地址
做
“#将值添加到报告表中
ws(1).Cells(r,1)=.Cells(district.Row,1)的子编号
ws(1).Cells(r,2)=.Cells(region.Row,6)的父名称
ws(1).Cells(r,3)=.Cells(region.Row,3)的父编号
ws(1).Cells(r,4)=.Cells(district.Row,6)的子名称
ws(1).单元格(r,5)=.单元格(地区行,3)的子编号
ws(1).Cells(r,6)=.Cells(district.Row,5)的子名称
设置地区=.Range(“C:C”).FindNext(地区)
r=r+1
循环而非区域是Nothing和district。首先寻址district
如果结束
设置区域=.Range(“C:C”).FindNext(区域)
循环而非区域为Nothing和region.Address regionfirst
如果结束
下一个k
以
端接头
使用两个Range.Find和FindNext导致错误。到达第二个FindNext(region)时,返回值等于第一个FindNext(district),即使它正在查找region


此外,dim as区域同时创建错误消息

我认为问题可能是因为
.Range(“C:C”)
创建了新的范围。您可以尝试将其设置为一个范围
Set rngC=.range(“C:C”)
,然后调用其上的finds
rngC.Find
rngC.FindNext
实际上,据我所知,这两个
都是这样。FindNext
将查找传递给
之前的上一次调用的相同值,因此,您必须使用类似于
.Find(k,After:=region)
的内容,而不是
.FindNext(region)
@Slai,我相信您是正确的。将行更改为Set region=rng.Find(region,after:=region),这一切似乎都正常!!我认为问题可能是因为
.Range(“C:C”)
创建了新的范围。您可以尝试将其设置为一个范围
Set rngC=.range(“C:C”)
,然后调用其上的finds
rngC.Find
rngC.FindNext
实际上,据我所知,这两个
都是这样。FindNext
将查找传递给
之前的上一次调用的相同值,因此,您必须使用类似于
.Find(k,After:=region)
的内容,而不是
.FindNext(region)
@Slai,我相信您是正确的。将行更改为Set region=rng.Find(region,after:=region),这一切似乎都正常!!