Excel 2013 - 根據特定儲存格參考刪除行

Excel 2013 - 根據特定儲存格參考刪除行

我正在嘗試弄清楚如何刪除 Excel 2013 中單元格內容以數字開頭的行。

EG 在 HI 列中有多個條目,有些以字母開頭,有些以數字開頭。我想刪除 H 列中單元格內容以數字開頭的所有行。

任何指導/建議將不勝感激!

好的,我創建了一些 VBA 程式碼,它似乎可以在一張紙上運行:

Sub Macro_01()
Dim L As Long, i As Long
L = Cells(Rows.Count, "F").End(xlUp).Row
For i = L To 1 Step -1
    If Left(Cells(i, "F"), 1) Like "[0-9]" Or Cells(i, "F") = "" Then
        Rows(i).Delete
    End If
Next
End Sub

我正在嘗試弄清楚如何讓它在工作簿中的所有 284 張工作表上運作。我嘗試過 Do/Loop,但它只是無休止地運行(並停止響應)。因為我知道有多少張紙,所以我需要在程式碼中輸入一個簡單的指令嗎?

答案1

這是如何運作的? (我假設您發布的程式碼本身按照您的預期工作,只需在全部床單。

Sub Macro_01()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Dim L As Long, i As Long
Dim ws      As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    L = .Cells(Rows.Count, "F").End(xlUp).Row
    For i = L To 1 Step -1
        If Left(.Cells(i, "F"), 1) Like "[0-9]" Or .Cells(i, "F") = "" Then
            .Rows(i).Delete
        End If
    Next
Next ws

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

只需循環遍歷每個工作表,確保重置最後一行變數。然後轉動計算/螢幕更新直到結束。請注意,如果您有 200 多張紙,這可能確實需要一些時間。

最後,請確保首先在資料副本上執行此命令,因為您可能知道,您無法撤銷巨集。

答案2

感謝您到目前為止的幫助,我想我已經解決了:

Sub Macro_01()
ActiveSheet.Next.Select
Do
Dim L As Long, i As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
L = Cells(Rows.Count, "F").End(xlUp).Row
For i = L To 1 Step -1
If Left(Cells(i, "F"), 1) Like "[0-9]" Or Cells(i, "F") = "" Then
Rows(i).Delete
End If
Next
Next ws
If ActiveSheet.Index <> Sheets.Count Then
ActiveSheet.Next.Select
Else
Exit Do
End If
Loop
End Sub

相關內容