簡體   English   中英

Excel宏可以連續搜索3個項目,如果有則刪除該行?

[英]Excel Macro to search for 3 items in a row and delete that row if they are there?

我真的很陌生,到目前為止我做了幾個宏,但是相比之下,它們很容易。 我認為這需要實際編程。

我需要在A列中搜索一個詞。 然后,如果有,我需要在B列中搜索格式。 然后,如果這些都是真的,我需要在C列中搜索一個詞。 如果所有3個都為真,則需要刪除該行。 有人可以幫忙嗎?

到目前為止,我已經有了這段代碼,這是我在宏下擁有的唯一內容。 我以為我很近,但是沒有用。

Dim lngRow As Long 
Dim lngRows As Long 
'Find the last row in Column A 
lngRows = Range("A" & Rows.Count).End(xlUp).Row 

For lngRow = lngRows To 2 Step -1 
    If ThisWorkbook.Sheets(1).Cells(lngRow, "A").Value = "No Longer Employed" And ThisWorkbook.Sheets(1).Cells(lngRow, "E").Style = "Bad" And ThisWorkbook.Sheets(1).Cells(lngRow, "I").Value = "Hire Date" Then
       ThisWorkbook.Sheets(1).Rows(lngRow).EntireRow.Delete 
    End If
Next

嘗試這個...

Dim indexRow As Long
Dim LastRow As Long

Dim keyA, keyE, keyI As String
Dim colA, colE, colI As Integer


keyA = "No Longer Employed"
keyE = "Bad"
keyI = "Hire Date"

colA = 1 ' index of col A
colE = 5 ' index of col E
colI = 9 ' index of col I


indexRow = 2 ' starting Row number from where start the loop 

'Find the last row in Column A
LastRow = Range("A" & Rows.Count).End(xlUp).Row


For indexRow = indexRow To LastRow Step 1 ' this loop increments the indexRow  value

If ActiveSheet.Cells(indexRow, colA).Value = keyA Then
   If ActiveSheet.Cells(indexRow, colE).Value = keyE Then
    If ActiveSheet.Cells(indexRow, colI).Value = keyI Then

            ActiveSheet.Rows(indexRow).EntireRow.Delete
            indexRow = indexRow - 1 ' because we have just deleted one row

        End If
    End If
   End If

Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM