2014-11-04 72 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