【问题标题】:.Findnext not respecting the range set.Findnext 不尊重范围集
【发布时间】:2018-12-11 12:09:43
【问题描述】:

在我的代码中,我使用了两个 .Find 实例,以及一个 .FindNext。众所周知,这很容易出错,不幸的是我也不例外。然而,这是我能想到的最好的。下面的代码,我去掉了最不相关的东西。

问题是有重复的值,我想保留两个,所以我决定使用.Findnext如果有重复,使用:

If newqst = refqst Then
    Set newqstadrs = Findrange.FindNext(after:=lstqstadrs)
Else

这里的问题是.FindNext 不尊重它应该继续使用Findrange.Find,而是继续使用这里使用的FindRangeTwo.Find

newrowtwo = FindRangeTwo.Find(rCell.Value, LookIn:=xlValues, lookat:=xlWhole).row

完整代码:

For o = 72 To lastrow 

    Dim refqst As String
    refqst = wss.Cells(o, 1).Value
    If Not refqst = "" Then
        If InStr(refqst, ".") > 0 Then
            Dim Findrange As Range
            Dim newqst As String
            Dim newqstadrs As Range
            Dim lstqstadrs As Range

            If newqst = refqst Then
                Set newqstadrs = Findrange.FindNext(after:=lstqstadrs)
            Else

                Select Case Left(refqst, 1)
                    Case 1
                        Set Findrange = wsa.Range(wsa.Cells(4, gewaskolom), wsa.Cells(11, gewaskolom))
                    'some more cases here
                End Select
                Set newqstadrs = Findrange.Find(refqst, LookIn:=xlValues, lookat:=xlWhole)
            End If

            If newqstadrs Is Nothing Then
            Else
                newqst = newqstadrs.Value
                Dim newrow As Long
                newrow = Findrange.Find(refqst, LookIn:=xlValues, lookat:=xlWhole).row
                Dim lstqst As String

                If Not wsa.Cells(newrow, 1) = "" Then
                    'do some stuff         
                    lstqst = refqst
                    Set lstqstadrs = newqstadrs

                ElseIf Not wsa.Cells(newrow, 2) = "" Then

                    Dim FindRangeTwo As Range
                    Set FindRangeTwo = wsa.Range(wsa.Cells(newrow, gewaskolom), wsa.Cells(wsa.Range("B" & newrow).End(xlDown).row, gewaskolom))
                    Dim SearchRange As Range
                    Set SearchRange = wss.Range(wss.Cells(o + 1, 1), wss.Cells(wss.Range("B" & o).End(xlDown).row, 1))
                    Dim rCell As Range
                    For Each rCell In SearchRange
                        Dim newrowtwo As Long
                        newrowtwo = FindRangeTwo.Find(rCell.Value, LookIn:=xlValues, lookat:=xlWhole).row
                        'do some more stuff
                    Next rCell
                    lstqst = refqst
                    Set lstqstadrs = newqstadrs
                End If
            End If            

        End If
    End If
Next o

【问题讨论】:

  • 您只能拥有一对 Find/FindNext。第二个覆盖第一个。您需要 FindRangeTwo 的替代方法。鉴于 FindRangeTwo 是单列 (gewaskolom),application.match 应该做得很好。
  • 我很害怕。回到绘图板。

标签: vba excel find next


【解决方案1】:

您只能拥有一对 Find/FindNext。第二个覆盖第一个。您需要 FindRangeTwo 的替代方法。鉴于 FindRangeTwo 是单列 (gewaskolom) 并且您正在寻找行,application.match 应该做得很好。

类似的,

dim newrowtwo as variant   '<~~ should be variant type for IsError to catch

...
newrowtwo = application.match(rCell.Value, FindRangeTwo, 0)
if not iserror(newrowtwo) then
    ...
end if
...

请注意,application.match 返回的是 FindRangeTwo 中的位置,而不是工作表上的行。工作表上的实际行是 (newrowtwo + newrow - 1)。

【讨论】:

  • iserror是为了捕捉到找不到匹配的事件?
  • 没错。如果找到,则抛出到变体的 Application.Match 将返回一个行号(匹配范围内的位置),如果没有,则返回一个可以用 IsError 捕获的错误。必须是一个变体,不能与 worksheetfunction.match 一起使用。
  • 谢谢,只是为了确保我将newrowtwo = newrowtwo + newrow -1 放在if not iserror 中,它就像一个魅力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多