2014-11-04 32 views
0

在我有一个Do循环中,我想使用'find'函数,但是当我尝试这样做时,它似乎在一次迭代后使我不在循环中。Excel宏 - 在Do循环中使用查找

仅供参考,我不是伟大的VB,与网页代码如更好:PHP等

如果我注释掉开头的行sfFamilyCol = wsSFDC.Rows(1)......那么循环将尽可能多地运行。在那里那行,它不会抛出一个错误,但它只会经历一次。

我想知道是否有一些特殊的处理方式。找到一个循环内?还是别的什么我不知道?任何帮助表示赞赏...

Option Explicit 

Sub mapTags() 

Dim wsMP As Worksheet: Set wsMP = ActiveWorkbook.Sheets("MP") 
Dim wsSFDC As Worksheet: Set wsSFDC = ActiveWorkbook.Sheets("SFDC") 
Dim wsMap As Worksheet: Set wsMap = ActiveWorkbook.Sheets("Mapping") 
Dim wsUp As Worksheet: Set wsUp = ActiveWorkbook.Sheets("Upload") 
Dim wsCol As Worksheet: Set wsCol = ActiveWorkbook.Sheets("MP_Columns") 
Dim wsFmt As Worksheet: Set wsFmt = ActiveWorkbook.Sheets("Tag Name Formats") 

Dim i As Long, j As Long, k As Long 
Dim SFDCrow As Long, SFDCID As String, sfCol As Long 
Dim MPID As String, mpTagGroup As String, mpTagName As String, mpTagCol As Long, mapTagName As String 
Dim sfTagFamily As String, sfTagGroup As String, sfTagName As String, sfFamilyCol As Long, sfGroupCol As Long 
Dim oRange As Range, aCell As Range, bCell As Range 

Application.ScreenUpdating = False 

''Get Contact Record 
For i = 2 To 2 Step -1 'Change i to 25000 later... 

MPID = wsMP.Cells(i, 1).Value 
sfCol = wsSFDC.Columns(2).Find(What:=MPID, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Row 
'MsgBox sfCol 

''Go Through each MP Contact Tag Colum 
For k = 10 To 1 Step -1 
    mpTagGroup = wsCol.Cells(k, 1).Value 
    If Not mpTagGroup = "" Then 
     mpTagCol = wsMP.Rows(1).Find(What:=mpTagGroup, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column 

     '' Get the Tag Name 
     mpTagName = wsMP.Cells(i, mpTagCol).Value 
     If Not mpTagName = "" Then 

      ''Get the Mapped SFDC Tags 
      Set oRange = wsMap.Columns(1) 
      Set aCell = oRange.Find(What:=mpTagGroup, LookIn:=xlValues, _ 
      LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False) 
      'MsgBox mapTagGroup 

      If Not aCell Is Nothing Then 
       Set bCell = aCell 
       'FoundAt = aCell.Row 
       Do 
        Set aCell = oRange.FindNext(After:=aCell) 

        If Not aCell Is Nothing Then 
         If aCell.Row = bCell.Row Then Exit Do 
         'FoundAt = FoundAt & ", " & aCell.Row 
         mapTagName = wsMap.Cells(aCell.Row, 2).Value 
         If mapTagName = mpTagName Then 
          sfTagFamily = wsMap.Cells(aCell.Row, 4).Value 
          sfTagGroup = wsMap.Cells(aCell.Row, 5).Value 
          sfTagName = wsMap.Cells(aCell.Row, 6).Value 

          'MsgBox aCell & " " & mapTagName & ": " & sfTagFamily & " " & sfTagGroup & " " & sfTagName 
          MsgBox sfTagFamily 

          ''Set the SDDC TAG FAMILY to TRUE 
          sfFamilyCol = wsSFDC.Rows(1).Find(What:=sfTagGroup, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column 
          MsgBox sfFamilyCol 

         End If 
        Else 
         Exit Do 
        End If 
       Loop 
      End If 

      'MsgBox "The Search String has been found in these rows: " & FoundAt 


     End If 
    End If 
Next k 
Next i 

End Sub      
+0

这是[好文章](http://www.cpearson.com/excel/FindAll.aspx)所以从这里开始。我认为你只需要清理使用* Find Method *的顺序。另外,尝试通过添加断点来隔离哪个部分不起作用,以便我们可以专注于该行。 – L42 2014-11-04 22:51:56

+0

另外[链接](http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/)为您的阅读乐趣 – 2014-11-05 02:17:03

回答

0

据我所知,只有一个.Find想起操作和任何后续.FindNext将使用最后.Find的参数。您的Set aCell = oRange.FindNext(...在第二次迭代期间使用sfFamilyCol = wsSFDC.Rows(1).Find(...

sfFamilyCol = wsSFDC.Rows(1).Find(...使用lookat:=xlPart但也许你可以把它改成类似,

if application.countif(wsSFDC.Rows(1), chr(42) & sfTagGroup & chr(42)) then _ 
    sfFamilyCol = application.match(chr(42) & sfTagGroup & chr(42), wsSFDC.Rows(1), 0) 

这应该是足够的错误控制,只尝试通配符Match如果它看起来像它可以被发现。随后致电Set aCell = oRange.FindNext(...应保持不受影响。

+0

非常感谢,我没有意识到。 FindNext将使用新的.Find。 使用.Match代替.Find效果很好 – Sanfly 2014-11-05 00:26:39