2016-02-03 45 views
-4

我的代碼如下:我試圖向計添加到我的代碼中發現搜索次數

Dim ws As Worksheet 
Dim ExitLoop As Boolean 
Dim SearchString As String, FoundAt As String 


Set ws = Worksheets("detail_report") 


On Error GoTo Err 


Set oRange = ws.Cells 

SearchString = "front input" 

Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False) 

If Not aCell Is Nothing Then 
    Set bCell = aCell 
    FoundAt = aCell.Address 
    Do While ExitLoop = False 
     Set aCell = oRange.FindNext(After:=aCell) 

     If Not aCell Is Nothing Then 
      If aCell.Address = bCell.Address Then Exit Do 
      FoundAt = FoundAt & ", " & aCell.Address 
     Else 
      ExitLoop = True 
     End If 
    Loop 
Else 
    MsgBox SearchString & " not Found" 
End If 

MsgBox "The Search String has been found these locations: " & FoundAt 
Exit Sub 
Err: 
MsgBox Err.Description 


End Sub 

我加入了下面的代碼,但不知道如何添加計數器部分:

Dim S As String 
Dim count As Integer 
+0

這不是很清楚 - 你能解釋一下你想要的東西的櫃檯,你希望它是如何工作的? –

+0

計算如何在Excel工作表中找到搜索字符串中的單詞@ Grade'Eh'Bacon – Dayday

+0

我認爲如果您解釋(用語言,而不是VBA代碼)您嘗試使用您的程序完成的工作將會有所幫助。現在,我實在無法告訴它發生了什麼事,而且你已經制定了以「如果不適合」開頭的結構......這讓我感到困惑。就目前來看,我不認爲這會在結束之前進行超過1場比賽[雖然我可能會誤解快速閱讀],所以看起來它並沒有達到你期望的水平。 –

回答

1

隨着當前的代碼,你甚至不需要使用計數器。相反,您可以將FoundAt加載到數組中,然後使用Ubound來計算總數。 請注意,您必須添加1,因爲數組基於0。

之前添加這些行最終Msgbox

Dim iCount() as String 
iCount = Split(FoundAt,", ") 

MsgBox "The Search String has been found " & UBound(iCount)+1 & " times at these locations: " & FoundAt 
+0

謝謝你的工作 – Dayday

相關問題