我有一個長的 excel 檔案,我需要從中洗掉包含 A 列中某些值的整行。該列包含從 [PAR001 到 PAR0023247] 的值,我需要洗掉屬于 [PAR002537 到 PAR005214] 的值是最快的方法嗎?
uj5u.com熱心網友回復:
嘗試搜索“PAR”的范圍并使用 val() 方法比較整數的不同長度,而不必搜索可能是右側整數的一部分的“0”。
向后計數范圍很重要。此外,如果您在相關串列上方有任何以“PAR”開頭的單元格,則可能存在問題。
這對我有用。我希望這個對你有用。
Public Sub DeleteRows()
Dim arr As Range
Dim tester As String
Dim NumElements As Long
Set arr = Range(Range("A1"), Cells(Rows.Count, 1).End(xlUp))
NumElements = arr.Cells.Count
For i = NumElements To 1 Step -1
If Left(arr(i), 3) = "PAR" Then
tester = Right(arr(i), Len(arr(i)) - 3)
If Val(tester) >= 2537 And Val(tester) <= 5214 Then
Cells(i, 1).EntireRow.Delete
End If
End If
Next i
End Sub
uj5u.com熱心網友回復:
老實說,我認為@haplo76 的回答非常好。
僅當您的資料按這些 PARxxxx 值升序排序時,我的答案才有效。如果沒有,請使用@haplo76 回答
此解決方案的唯一優點是您無需回圈并在一行代碼中洗掉所有行。
在我的測驗中,我洗掉了行 [PAR03 到 PAR010]

Sub TEST()
Dim i_row As Long
Dim e_row As Long
'In e_row you want to match the value AFTER the last one you want to to delete
' so if you want to delete from PAR003 to PAR010
' i_row will search for PAR003 but e-row need to search for PAR011!!!
With Application.WorksheetFunction
i_row = .Match("PAR003", Range("A:A"), 0)
e_row = .Match("PAR011", Range("A:A"), 0)
End With
Range("A" & i_row & ":A" & e_row).Delete
End Sub
執行代碼后,我得到:

另一個優點是您可以輕松地將這個子轉換為一個通用的,只要求洗掉 PARxx 值:
Sub TEST()
DELETE_ROWS "PAR003", "PAR011" 'this will delete [PAR003 to PAR010]
End Sub
Sub DELETE_ROWS(ByVal ini_val As String, end_val As String)
Dim i_row As Long
Dim e_row As Long
With Application.WorksheetFunction
i_row = .Match(ini_val, Range("A:A"), 0)
e_row = .Match(end_val, Range("A:A"), 0)
End With
Range("A" & i_row & ":A" & e_row).Delete
End Sub
But as I said at first, this will work only if your data is sorted in column A and ascending order. Also, if you type a "PAR00xx" value not found, it will raise an error.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456767.html
上一篇:如何在電子表格中總結結果
