2017-02-12 117 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

您需要通過添加.作爲前綴完全限定所有RangeCellsWith WS語句中。

E.g.而不是pattern = Cells(10, 2)使用pattern = .Cells(10, 2),.Cells(10, 2)意味着WS的單元格(10,2),它在For Each WS In ThisWorkbook.Worksheets中正在進行中。

代碼

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:不要使用兩個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

首先,感謝你的善意幫助。我嘗試了第二個代碼,它的效果很好,但它僅刪除了所有工作表中的單元格(10,2)中的匹配單詞,並忽略了其他列或行中的任何其他類似單詞。更好地表達自己。說我想刪除任何有紅蘋果這個詞的表中的任何一行。那麼請你協助? –

+0

@NabilAmer你想從整個工作表中刪除'Cells(10,2)'中的單詞嗎?導致你的帖子你只看A到C列 –

+0

嗨嗨,我試過編輯2,它只在單元格(10,2)有要求的單詞時纔有效,但如果沒有找到,它就不能在其他單上使用。單元格(10,2)在sheet1中需要排除,所以我添加了下面的代碼,它可以根據需要工作(pattern = Sheets(「Sheet1」)。單元格(10,2)以及這個(如果WS。如果可以的話,我還有一個問題,如何在不要求我重複輸入所需數據的情況下如何使用輸入框,因爲我使用的代碼也是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