在我的Do循环中,我想使用' find'函数,但是当我尝试这个时,它似乎会在一次迭代后让我不在循环中。
仅供参考,我在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 :(得分:0)
AFAIK,只记住了一个.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(...
的后续调用应保持不受影响。