2014-10-20 42 views
0

我正在使用此循环来查找值。找到工作,但findNext不会,省略许多值。在这里我放下我的代码,你有什么建议吗?非常感谢你!!。在循环中找到下一个不工作

For Each ws In SourceWb.Worksheets 
    If IsNumeric(Left(ws.Name, 3)) Then 
     Set gCell = ws.Columns(6).Find(what:=numdoc, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, searchformat:=False) 
     If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then 

      firstAddress = gCell.Address 

      Do 
       repetidos = repetidos + 1 
       finalcell = gCell.Address 
       'merged cells code here not displayed 
       oldaddress = gCell.Address 
       '>Having trouble here> ** 
       Set gCell = ws.Columns(6).FindNext(after:=gCell) 
       '** 


      Loop Until gCell.Address = oldaddress 
     End If 
    End If 
Next ws 
+0

这是为你编译的吗?我得到一个'没有For'编译错误。 – 2014-10-20 09:00:51

+0

是的,确实如此,如果在发帖时尝试删除以下代码语句:if firstAddress <> oldaddress Then – Chakal 2014-10-20 09:09:05

+0

您是如何声明变量的?我不认为这是问题,但我很难复制你的问题。你看过你的“Do ... Loop”中的逻辑吗? – 2014-10-20 09:53:28

回答

0

这是我可以从你的线索得到最好的:

Option Explicit 

Sub Test() 
Dim WS As Worksheet 
Dim SourceWB As Workbook 
Dim numdoc As Long 
Dim gCell As Range 
Dim firstAddress As String 
Dim oldaddress As String 
Dim finalcell As String 
Dim repetidos As Long 

Set SourceWB = ThisWorkbook 'added for clarity and safety 
numdoc = 456 
    For Each WS In SourceWB.Worksheets 
     If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A" 
      Set gCell = WS.Columns(6).Find(what:=numdoc, _ 
       LookIn:=xlValues, _ 
       lookat:=xlWhole, _ 
       SearchOrder:=xlByRows, _ 
       MatchCase:=False, _ 
       searchformat:=False) 
      If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then 
       firstAddress = gCell.Address 
       Set gCell = WS.Columns(6).FindNext(after:=gCell) 
       Do 
        repetidos = repetidos + 1 
        finalcell = gCell.Address 
        'merged cells code here not displayed 
        oldaddress = gCell.Address 
       Loop Until gCell.Address = oldaddress 
      End If 
     End If 
    Next WS 
End Sub 

,如果它回答了这个问题,但是,它确实说明压痕不知道。

那里可能有With...End With的空间,但我太累了,找不到它。

+0

可能是(with)我会寻找它并尝试将它放在sourcewb.worksheets中的每个ws之前。非常感谢你 – Chakal 2014-10-20 11:01:51

+0

尝试尽可能高的结构,你可以。不要忘了点'.' – 2014-10-20 11:40:39

+0

蜱会很好:) – 2014-10-20 11:53:26

0
This seems to work at this point. 

For Each WS In SourceWB.Worksheets 
    With ws.Range("F:F") 
    If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A" 
     Set gCell = .Find(what:=numdoc, _ 
      LookIn:=xlValues, _ 
      lookat:=xlWhole, _ 
      SearchOrder:=xlByRows, _ 
      MatchCase:=False, _ 
      searchformat:=False) 
     If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then 
      firstAddress = gCell.Address 
      Set gCell = .FindNext(after:=gCell) 
      Do 


       'merged cells code here not displayed 

      Loop While Not gCell Is Nothing And gCell.Address <> firstAddress 
     End If 
    End If 
end with 
Next WS