繁体   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