簡體   English   中英

如果它們在數值范圍內,如何清除VBA中的單元格內容

[英]How to clear cell contents in VBA if they are within a range of numeric values

我正在嘗試清除包含數字1-12的列中任何單元格的內容。 我目前正在使用for循環和if語句,一次遍歷數字1-12,如果單元格包含這些值,則清除內容。 我正在使用的文件具有35,000多行數據; 有沒有一種更有效的方法來告訴宏刪除這些數字而不為它們創建單獨的elseif語句?

For r = 2 To i
    If Cells(r, 1).Value = "1" Then
        Cells(r, 1).ClearContents
        ElseIf Cells(r, 1).Value = "2" Then
        Cells(r, 1).ClearContents

您可以將比較運算符與And結合使用:

For r = 2 to i
    If Cells(r, 1).Value >= 1 And Cells(r, 1).Value <= 12 Then
        Cells(r, 1).ClearContents
    End If
Next

使用過濾器:

Sub tgr()

    Dim ws As Worksheet
    Set ws = ActiveWorkbook.ActiveSheet

    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    With ws.Range("A1", ws.Cells(ws.Rows.Count, "A").End(xlUp))
        .AutoFilter 1, ">=1", xlAnd, "<=12"
        .Offset(1).ClearContents
        .AutoFilter
    End With

    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

暫無
暫無

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

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