Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果为0,则清除多列的内容_Excel_Vba - Fatal编程技术网

Excel 如果为0,则清除多列的内容

Excel 如果为0,则清除多列的内容,excel,vba,Excel,Vba,我想知道如何使以下代码适用于多列(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 您可以使用范围(“D5:

我想知道如何使以下代码适用于多列(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 

您可以使用
范围(“D5:P65536”)。替换内容:=0,替换:=”
一次替换所有内容。

可能您正在寻找类似的内容。只有当一行中的每个单元格都包含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()
方法执行搜索。如果您的值存在于该范围内(range=column),则可以通过这种方式清除内容

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

你要找的“0”到底在哪里?我觉得有一种更简单的方法,但现在你没有提供太多信息。还有,你的“0”在哪里“格式化为字符串?因为这就是你的意思,把它放在引号之间。有没有什么原因你不能使用嵌套循环,只按数字调用它们?为什么要使用Countif来测试一个单元格是否包含“0”,并且只在每个单元格都包含0的情况下清除内容,还是在只有一个单元格包含0的情况下清除内容?非常简单。非常感谢你!