簡體   English   中英

如果為0,則清除多列內容

[英]If 0 clear contents for multiple columns

我想知道如何使以下代碼適用於多列(D:P)? 我已經嘗試將&“:” P“&” 65536“添加到范圍中,但沒有成功。

For i = 5 To Range("D" & "65536").End(xlUp).Row Step 1
If Application.WorksheetFunction.CountIf(Range("D" & i), "0") = 1 Then
Range("D" & i).ClearContents
End If
Next i 

您可以使用Range("D5:P65536").Replace What:=0,Replacement:=""可一次替換所有內容。

也許您正在尋找類似的東西。 僅當一行中的每個單元格都包含0時,這才會清除內容。

Dim i As Long
Dim rg As Range

    For i = 5 To Range("D" & "65536").End(xlUp).Row Step 1
        Set rg = Range("D" & i & ":P" & i)
        If Application.WorksheetFunction.CountIf(rg, "0") = 13 Then
            rg.ClearContents
        End If
    Next i

遍歷實際的列,並使用Find()方法執行搜索。 如果您的值在該范圍內(范圍=列),則可以通過這種方式清除內容。

Sub test()

    ' Just for illustration on the columns
    Const D& = 4:   Const P& = 16

    Dim ws As Worksheet, col As Long
    Set ws = ThisWorkbook.Worksheets(1)

    For col = D To P
        If Not ws.Columns(col).find(What:="0", LookAt:=xlWhole) Is Nothing Then
            ws.Columns(col).ClearContents
        End If
    Next col

End Sub

暫無
暫無

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

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