2017-08-15 97 views
0

我寫了一個宏來刪除包含其中某些文本的行。如果任一關鍵字包含任何文本,宏將刪除該行。但是,這個宏根本不起作用。也許,我做了錯誤的事情。希望有人能幫我糾正這一點。提前致謝。無法刪除文本中包含某些關鍵字的行

以下是我與努力:

Sub customized_row_removal() 
    Dim i As Long 
    i = 2 
    Do Until Cells(i, 1).Value = "" 
     If Cells(i, 1).Value = "mth" Or "rtd" Or "npt" Then 
      Cells(i, 1).Select 
      Selection.EntireRow.Delete 
     End If 
     i = i + 1 
    Loop 
End Sub 

我正在尋找在刪除文本中的關鍵字:

AIRLINE DRIVE OWNER mth 
A rtd REPAIRS INC 
AANA MICHAEL B ET AL 
ABASS OLADOKUN 
ABBOTT npt P 
AIRLINE AANA MTH 
ABASS REPAIRS NPT 

回答

1

嘗試這樣的。
使用Lcase怎麼樣?

Sub customized_row_removal() 
    Dim rngDB As Range, rngU As Range, rng As Range 
    Dim Ws As Worksheet 

    Set Ws = Sheets(1) 
    With Ws 
     Set rngDB = .Range("a2", .Range("a" & Rows.Count)) 
    End With 

    For Each rng In rngDB 
     If InStr(LCase(rng), "mth") Or InStr(LCase(rng), "rtd") Or InStr(LCase(rng), "npt") Then 
      If rngU Is Nothing Then 
       Set rngU = rng 
      Else 
       Set rngU = Union(rngU, rng) 
      End If 
     End If 
    Next rng 
    If rngU Is Nothing Then 
    Else 
     rngU.EntireRow.Delete 
    End If 
End Sub 
+0

謝謝Dy.Lee,它適用於小寫。怎麼樣上層或混合的情況?謝謝。 – SIM

+0

@Shahin,它在任何情況下工作 –

+0

對不起Dy.Lee,因爲我的無知。引號內的文字必須用小寫字母表示,如我不明白的「mth」而不是「MTH」。 – SIM

1
Or

VBA語法是錯誤的,

If Cells(i, 1).Value = "mth" Or "rtd" Or "npt" Then 

應該是:

If Cells(i, 1).Value = "mth" Or Cells(i, 1).Value = "rtd" Or Cells(i, 1).Value = "npt" Then 

但是,你需要使用一個字符串函數,如InstrLike,看是否有特定的字符串是一個較長的字符串內找到。

代碼

Option Explicit 

Sub customized_row_removal() 

Dim WordsArr As Variant 
Dim WordsEl As Variant 
Dim i As Long, LastRow As Long 
Dim Sht As Worksheet 

WordsArr = Array("mth", "rtd", "npt") 

Set Sht = Worksheets("Sheet1") 
With Sht 
    ' get last row in column "A" 
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 
    For i = LastRow To 2 Step -1 
     For Each WordsEl In WordsArr 
      If LCase(.Cells(i, 1).Value) Like "*" & WordsEl & "*" Then 
       .Rows(i).Delete 
      End If 
     Next WordsEl 
    Next i 
End With 

End Sub  
+0

感謝夏嘉曦瑞士雷達表,你的答案。你的宏部分工作。然而,它不能處理兩件大事。 1.不能觸摸含有大小寫混合或大寫的相同關鍵字的單元格。我將它應用於500個細胞。第一次運行時,它刪除20,然後再次運行時刪除另一個10等,直到它們都包含關鍵字。 – SIM

+0

@Shahin嘗試編輯的代碼 –

+0

我剛剛嘗試,但糾正的代碼根本不起作用 – SIM

0

我儘量讓我的代碼示例,我可以,如果你有任何問題,請諮詢

Private Sub remove_word_raw() 
'PURPOSE: Clear out all cells that contain a specific word/phrase 

Dim Rng As Range 
Dim cell As Range 
Dim ContainWord As String 

'What range do you want to search? 

    Set Rng = Range("A2:A25") 

    'sub for the word 

    shorttext1 = "mth" 
    shorttext2 = "rtd" 
    shorttext3 = "npt" 
'What phrase do you want to test for? 

    ContainWord1 = shorttext1 
    ContainWord2 = shorttext2 
    ContainWord3 = shorttext3 

'Loop through each cell in range and test cell contents 

    For Each cell In Rng.Cells 
    If cell.Value2 = ContainWord1 Then cell.EntireRow.Delete 

    Next 
    For Each cell In Rng.Cells 
    If cell.Value2 = ContainWord2 Then cell.EntireRow.Delete 

    Next 
    For Each cell In Rng.Cells 

     If cell.Value2 = ContainWord3 Then cell.EntireRow.Delete 
     Next cell 
End Sub 
相關問題