2011-01-19 24 views
5

我寫了一個宏,它將在excel文件的所有表格中搜索字符串。該宏將激活第一個工作表以及包含搜索字符串的工作表中的單元格。如果沒有找到,它會顯示一條消息。這個宏運行良好。我想擴展這個功能來覆蓋包含這個字符串而不是第一個的所有表單。所以我修改了宏,但沒有按預期工作。我已經給出了下面的代碼,並在它顯示錯誤的地方發表了評論。使用宏在excel文件的所有表格中搜索字符串

 
Dim sheetCount As Integer 
Dim datatoFind 

Sub Button1_Click() 

Find_Data 

End Sub 

Private Sub Find_Data() 
    Dim counter As Integer 
    Dim currentSheet As Integer 
    Dim notFound As Boolean 
    Dim yesNo As String 

    notFound = True 

    On Error Resume Next 
    currentSheet = ActiveSheet.Index 
    datatoFind = InputBox("Please enter the value to search for") 
    If datatoFind = "" Then Exit Sub 
    sheetCount = ActiveWorkbook.Sheets.Count 
    If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind) 
    For counter = 1 To sheetCount 
     Sheets(counter).Activate 

     Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False).Activate 

     If InStr(1, ActiveCell.Value, datatoFind) Then 
      If HasMoreValues(counter + 1) Then 'Not completing the method and directly entering 
       yesNo = MsgBox("Do you want to continue search?", vbYesNo) 
       If yesNo = vbNo Then 
        notFound = False 
        Exit For 
       End If 
      End If 
      Sheets(counter).Activate 
     End If 
    Next counter 
    If notFound Then 
     MsgBox ("Value not found") 
     Sheets(currentSheet).Activate 
    End If 
End Sub 

Private Function HasMoreValues(ByVal sheetCounter As Integer) As Boolean 
    HasMoreValues = False 
    Dim str As String 

    For counter = sheetCounter To sheetCount 
     Sheets(counter).Activate 

     str = Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False).Value 'Not going further than this i.e. following code is not executed 

     If InStr(1, str, datatoFind) Then 
      HasMoreValues = True 
      Exit For 
     End If 
    Next counter 
End Function 


+0

很抱歉,但你的代碼有一些矛盾和我無法弄清楚你正在嘗試做什麼。請描述您的程序在工作表中存在多個匹配項時應該執行的操作。順便說一下,看看FindNext方法 – 2011-01-19 13:48:15

+0

我的代碼執行以下操作:1.搜索所有工作表中的第一個可用匹配項。如果找到,它將激活工作表和存在搜索字符串的單元格。 2.搜索下一個可用匹配。如果存在,則顯示msgbox,並且是/否表示存在更多搜索。你想繼續嗎?如果是,則下一個可用比賽如第1點所述顯示,然後搜索下一個可用比賽,直到所有比賽用盡。 3.如果沒有可用的匹配,則該過程停止。其方法是FindNext,「Cells」還是「Sheets」? – samar 2011-01-20 06:48:16

回答

4

我能解決我的問題,並已張貼的那些代碼誰可能需要

 

Dim sheetCount As Integer 
Dim datatoFind 

Sub Button1_Click() 

Find_Data 

End Sub 

Private Sub Find_Data() 
    Dim counter As Integer 
    Dim currentSheet As Integer 
    Dim notFound As Boolean 
    Dim yesNo As String 

    notFound = True 

    On Error Resume Next 
    currentSheet = ActiveSheet.Index 
    datatoFind = StrConv(InputBox("Please enter the value to search for"), vbLowerCase) 
    If datatoFind = "" Then Exit Sub 
    sheetCount = ActiveWorkbook.Sheets.Count 
    If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind) 
    For counter = 1 To sheetCount 
     Sheets(counter).Activate 

     Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False).Activate 

     If InStr(1, StrConv(ActiveCell.Value, vbLowerCase), datatoFind) Then 
      notFound = False 
      If HasMoreValues(counter) Then 
       yesNo = MsgBox("Do you want to continue search?", vbYesNo) 
       If yesNo = vbNo Then 
        Sheets(counter).Activate 
        Exit For 
       End If 
      Else 
       Sheets(counter).Activate 
       Exit For 
      End If 
      Sheets(counter).Activate 
     End If 
    Next counter 
    If notFound Then 
     MsgBox ("Value not found") 
     Sheets(currentSheet).Activate 
    End If 
End Sub 

Private Function HasMoreValues(ByVal sheetCounter As Integer) As Boolean 
    HasMoreValues = False 
    Dim str As String 
    Dim lastRow As Long 
    Dim lastCol As Long 
    Dim rRng As Excel.Range 

    For counter = sheetCounter + 1 To sheetCount 
     Sheets(counter).Activate 

     lastRow = ActiveCell.SpecialCells(xlLastCell).Row 
     lastCol = ActiveCell.SpecialCells(xlLastCell).Column 

     For vRow = 1 To lastRow 
      For vCol = 1 To lastCol 
       str = Sheets(counter).Cells(vRow, vCol).Text 
       If InStr(1, StrConv(str, vbLowerCase), datatoFind) Then 
        HasMoreValues = True 
        Exit For 
       End If 
      Next vCol 

      If HasMoreValues Then 
       Exit For 
      End If 
     Next vRow 

     If HasMoreValues Then 
      Sheets(sheetCounter).Activate 
      Exit For 
     End If 
    Next counter 
End Function 

問候,

薩馬

-1

問題是,Cells.Find返回一個範圍。當您在功能HasMoreValues使用它,使用它是這樣的:

Cells.Find(...).Value 

,但返回的範圍不會轉換爲.value的正確。您可以通過使用.text代替.value解決這個問題,就像這樣:

Cells.Find(...).text 

或全部:

str = Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
    False, SearchFormat:=False).text 

要完全正確的,你應該set的尋找範圍變量的結果,然後通過它訪問它,以防Find搜索返回什麼。然而根據文檔Cells.Find總是返回一個單元格的範圍,所以你可能會沒事的。