2017-02-12 20 views
0

このブックを使用して、同じブックの複数のシート内の異なる行の表のセル(10,2)に書き込まれた単語を検索し、各テーブルの行全体が削除されますが、コマンドボタンがオンで他のシートには適用されていない最初のシートにコードが適用されるので、この点を助けてください。複数のシート内の条件に基づいて行を削除する

sub Deletrows_Click() 

Dim WS As Worksheet 
Dim pattern As String 

For Each WS In ThisWorkbook.Worksheets 
    With WS 
     pattern = Cells(10, 2) ' delete row if found the word total in it 
     RowCount = ActiveSheet.UsedRange.Rows.Count 

     Dim i As Integer 
     For i = 2 To RowCount 
      Dim j As Integer 
      For j = 1 To 3 'find the word within this range 
       If Cells(i, j) = pattern Then 
        Cells(i, j).EntireRow.Delete 
       End If 
      Next j 
     Next i 
    End With 
Next WS 

End Sub 
+0

あなたの親切な支援のための多くのおかげでシャイ、それは、まずへの感謝をたくさん –

答えて

2

あなたは完全に接頭辞として.を追加することにより、With WS文の中のすべてのあなたのRangeCellsを修飾する必要があります。

など。 pattern = Cells(10, 2)の代わりにpattern = .Cells(10, 2)を使用すると、.Cells(10, 2)はで進められているWSのセル(10,2)を意味します。

コード

Option Explicit 

Sub Deletrows_Click() 

Dim WS As Worksheet 
Dim pattern As String 
Dim RowCount As Long, i As Long, j As Long 

For Each WS In ThisWorkbook.Worksheets 
    With WS 
     pattern = .Cells(10, 2) ' delete row if found the word total in it 
     RowCount = .UsedRange.Rows.Count 

     For i = 2 To RowCount 
      For j = 1 To 3 'find the word within this range 
       If .Cells(i, j) = pattern Then 
        .Cells(i, j).EntireRow.Delete 
       End If 
      Next j 
     Next i 
    End With 
Next WS 

End Sub 

オプション2:代わりに2つのForループを使用しての、あなたは行全体で一定の値を探すために、Application.Match機能と第二Forループを置き換えることができます。マッチと

コード

Option Explicit 

Sub Deletrows_Click() 

Dim WS As Worksheet 
Dim pattern As String 
Dim RowCount As Long, i As Long, j As Long 

For Each WS In ThisWorkbook.Worksheets 
    With WS 
     pattern = .Cells(10, 2) ' delete row if found the word total in it 
     RowCount = .UsedRange.Rows.Count 

     For i = 2 To RowCount 
      ' use the Match function to find the word inside a certain row 
      If Not IsError(Application.Match(pattern, .Range(.Cells(i, 1), .Cells(i, 3)), 0)) Then '<-- match was successful 
       .Cells(i, 1).EntireRow.Delete 
      End If        
     Next i 
    End With 
Next WS 

End Sub 

編集2

Option Explicit 

Sub Deletrows_Click() 

Dim WS As Worksheet 
Dim pattern As String 
Dim FirstRow As Long, RowCount As Long, i As Long, j As Long 
Dim FirstCol, ColCount As Long 

For Each WS In ThisWorkbook.Worksheets 
    With WS 
     pattern = .Cells(10, 2) ' delete row if found the word total in it 
     FirstRow = .UsedRange.Row 
     RowCount = .UsedRange.Rows.Count 
     FirstCol = .UsedRange.Column 
     ColCount = .UsedRange.Columns.Count 

     For i = 2 To RowCount + FirstRow 
      ' use the Match function to find the word inside a certain row 
      If Not IsError(Application.Match(pattern, .Range(.Cells(i, 1), .Cells(i, ColCount + FirstCol)), 0)) Then '<-- match was successful 
       .Cells(i, 1).EntireRow.Delete 
      End If 
     Next i 
    End With 
Next WS 

End Sub 
+0

に役立ちますあなたの親切な援助。私はそれがうまく動作する2番目のコードを試したが、それはすべてのシートのセル(10,2)で一致した単語を削除し、他の似た言葉を他の列や行で無視した。自分自身をより良く表現する。 「Red Apple」という単語が含まれているシートの行を削除したいとします。あなたは助けてくれますか? –

+0

@NabilAmerシート全体から 'Cells(10,2) 'にある単語を削除しますか?あなたの投稿が列A〜Cのみを見ているのです –

+0

こんにちは、編集2を試しましたが、セル(10,2)に必要な単語がある場合にのみ動作しますが、見つからなければ他のシートでは機能しません。セル(10,2)は除外する必要があるsheet1にありますので、次のコードを追加し、必要に応じて動作します(pattern = Sheets( "Sheet1"))。私はこのコードを使用しているので、必要なデータの入力を繰り返すことを要求せずに入力ボックスを使用することができますか?pattern = InputBox ( "選択したIDを入力してください。") –

0
Sub Deletrows_Click() 

Dim WS As Worksheet 
Dim pattern As String 
Dim FirstRow As Long, RowCount As Long, i As Long, j As Long 
Dim FirstCol, ColCount As Long 

For Each WS In ThisWorkbook.Worksheets 
    With WS 
     pattern = Sheets("Sheet1").Cells(10, 2) ' delete row if found the word in this source sheet 
     FirstRow = .UsedRange.Row 
     RowCount = .UsedRange.Rows.Count 
     FirstCol = .UsedRange.Column 
     ColCount = .UsedRange.Columns.Count 

     For i = 2 To RowCount + FirstRow 
      ' use the Match function to find the word inside a certain row 
      If WS.Name <> "Sheet1" Then 'I added this to exclude the said sheet as a source page 
      If Not IsError(Application.Match(pattern, .Range(.Cells(i, 1), .Cells(i, ColCount + FirstCol)), 0)) Then '<-- match was successful 
      .Cells(i, 1).EntireRow.Delete 
      End If 
      End If 
     Next i 
    End With 
Next WS 

End Sub 
関連する問題