2016-05-28 9 views
1

文字列を含む選択したセルの一覧をループし、各文字列を取り込み、その文字列のブック全体を検索するマクロを作成する必要があります。文字列が見つかった場合、サブは終了し、見つかった文字列を選択する必要があります。そうでなければ、リストの終わりまで見つかる次の文字列に移動します。Excel VBA - セルの列をループし、ブック内の各セルの値を検索します

私のコードを実行すると、リスト内の文字列が見つかってもそのセルに移動しないと、サブは終了しません。

Option Explicit 

Dim sheetCount As Integer 
Dim datatoFind 

Sub Button1_Click() 

Find_File 

End Sub 

Private Sub Find_File() 
Dim c As Range 
Dim counter As Integer 
Dim currentSheet As Integer 
Dim notFound As Boolean 
notFound = True 

For Each c In Selection.Cells 
    On Error Resume Next 
    currentSheet = ActiveSheet.Index 
    datatoFind = StrConv(c.Value, 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:=xlValues, LookAt _ 
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False).Activate 
     If InStr(1, StrConv(ActiveCell.Value, vbLowerCase), datatoFind) Then 
      notFound = False 
      Sheets(counter).Activate 
      Range("datatoFind").Select 
      Exit For 
     End If 
    Next counter 
    If notFound = True Then 
     MsgBox ("Value not found") 
     Sheets(counter).Activate 
    Else 
     Exit Sub 
    End If 
Next c 
End Sub 

ご協力いただきありがとうございます。

答えて

1

があります私のコードは、私は別の作ったが、ちょうど私が本当に役立っていること、

+0

ありがとう男...それは理解するのに役立つ願っていますあなたのコード

Public Sub Find_File() Dim c As Range Dim counter As Integer Dim currentSheet As Integer Dim notFound As Boolean Dim datatoFind As String Dim sheetCount As Integer Dim cellFound As Range Dim cellToFind As Range Dim originalSheet As Worksheet notFound = True Set originalSheet = ActiveSheet For Each c In Selection.Cells On Error Resume Next currentSheet = ActiveSheet.Index Set cellToFind = c datatoFind = StrConv(c.Value, 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 Set cellFound = Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlValues, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not cellFound Is Nothing Then If cellFound.Address <> cellToFind.Address And _ cellFound.Parent.Name <> cellToFind.Parent.Name Then notFound = False cellFound.Activate GoTo WorksEnd End If End If Next counter Next c WorksEnd: If notFound = True Then originalSheet.Activate MsgBox ("Value not found") End If End Sub 

劇的な変化はありません!あなたは私のリストがあった同じシートを検索する問題を解決し、発見されたセルにもっときれいにやって来る方法を見つけました。 – Ozone11

0

多くの問題があります。最初の1つは、すべてのシートを検索していることです。最初の試合はあなたの試合の列を含むシートの中にあるかもしれません!

シートをループする場合、発見されるアイテムの列を含むシートを除外するは...................た場合、他の問題

関連する問題