2011-01-19 101 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总是返回一个单元格的范围,所以你可能会没事的。