2016-08-11 6 views
0

このマクロはありますが、特定のシート(この場合は "aaa"、 "bbb"、 "ccc")でのみ検索を実行することはできません。\ 現在のコードはMsgBox "No紫色の欄が見つかりました」と言っていました。 私はまた、(現在他のシートが開いていても)最初に見つかったセルを選択して表示するためのマクロです。 助けてください。特定のシートで検索する

Dim cell As Range 
Dim SearchRange As Range 
Dim c As Range 
Dim shtfound As Boolean 
sthfound = False 
On Error Resume Next 
Set SearchRange = ThisWorkbook.Worksheets(Array("bbb", "aaa", "ccc")).UsedRange.SpecialCells(xlCellTypeVisible) 
On Error GoTo 0 
If Not SearchRange Is Nothing Then 
    With Application.FindFormat.Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 16711935 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 

    Set c = SearchRange.Find(What:="", After:=SearchRange.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _ 
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ 
    , SearchFormat:=True) 
    If Not c Is Nothing Then 
     firstAddress = c.Address 
     Set foundrange = c 
     Do 
      Set c = SearchRange.Find(What:="", After:=c, LookIn:=xlFormulas, LookAt:= _ 
      xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ 
      , SearchFormat:=True) 

      Set foundrange = Union(foundrange, c) 
     Loop While Not c Is Nothing And c.Address <> firstAddress 
     foundrange.Activate 
     sthfound = True 
     MsgBox "Purple fields found: " & foundrange.Count 


    End If 
End If 

If sthfound = False Then MsgBox "No purple field found" 
End Sub 
+0

なぜシートをループしないのですか?ループ内の各範囲を検索しますか? –

+0

Shai Radoさんが提案したのと同じことをお勧めします – Siva

+0

どういう意味ですか? – Pawel

答えて

2

ブック全体を検索し、該当するシートに含まれているかどうかを確認するだけです。ここでは例

Sub Sample() 
    Dim ws As Worksheet 
    Dim aCell As Range, bCell As Range 
    Dim searchString As String 

    '~~> This is your search string 
    searchString = "Sid" 

    '~~> Loop through the worksheet 
    For Each ws In ThisWorkbook.Worksheets 
     Set aCell = ws.Cells.Find(What:=searchString, _ 
           LookIn:=xlFormulas, _ 
           LookAt:=xlPart, _ 
           SearchOrder:=xlByRows, _ 
           SearchDirection:=xlNext, _ 
           MatchCase:=False, _ 
           SearchFormat:=False) 

     '~~> If found 
     If Not aCell Is Nothing Then 
      '~~> Check if it is in the sheet we want 
      Select Case aCell.Parent.Name 
      Case "aaa", "bbb", "ccc" 
       MsgBox "Found in Sheet " & aCell.Parent.Name 

       Set bCell = aCell 

       '~~> Find other occurances 
       Do 
        Set aCell = ws.Cells.FindNext(After:=aCell) 

        If Not aCell Is Nothing Then 
         If aCell.Address = bCell.Address Then Exit Do 
         MsgBox "Found in Sheet " & aCell.Parent.Name 
        Else 
         Exit Do 
        End If 
       Loop 
      End Select 
     End If 
    Next 
End Sub 

です:あなたはどのように.Find.FindNext作品を説明.Find and .FindNext In Excel VBAを見てみたいことがあります。

+0

これは私が必要としたものです。 – Pawel

1

残念ながら、別のシートの範囲を1つにまとめることはできません。私はあなたのシートを順番になり、最後にあなたの結果を追加し、上記のリンクで示唆したように VBA: How to combine two ranges on different sheets into one, to loop through

は、以下の質問は非常に関連性があると良い説明を与えます。

+0

問題は次のとおりです。どのようにシートを循環させるかわかりません:( – Pawel

関連する問題