Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
VBA基于特定条件的透明单元格范围_Vba_Cell - Fatal编程技术网

VBA基于特定条件的透明单元格范围

VBA基于特定条件的透明单元格范围,vba,cell,Vba,Cell,这个问题可能看起来很简单,但我已经处理了两天了,无法完成。我搜索了其他论坛,看看他们是否有同样的问题,但没有找到接近他的一个。 因此,我从第2行开始设置范围K:T,需要清除包含#N/A的单元格。我编写了下面的代码,但似乎无法使其正常工作 Sub Clear_cells() Dim rng As Range, i As Integer 'Set the range to evaluate to rng. Set rng = Range("K:T") 'Loop

这个问题可能看起来很简单,但我已经处理了两天了,无法完成。我搜索了其他论坛,看看他们是否有同样的问题,但没有找到接近他的一个。 因此,我从第2行开始设置范围K:T,需要清除包含#N/A的单元格。我编写了下面的代码,但似乎无法使其正常工作

Sub Clear_cells()
    Dim rng As Range, i As Integer

    'Set the range to evaluate to rng.
    Set rng = Range("K:T")

    'Loop backwards through the rows
    'in the range that you want to evaluate.
    For i = rng.Rows.count To 1 Step -1

        'If cell i in the range contains an "N/A", delete cell content
        If rng.Cells(i).Value = "#N/A" Then rng.Cells(i).CellRange.ClearContents (1)
    Next
End Sub

使用isna功能检查#N/A


如果单元格中包含错误值(即
\N/A
)而不是字符串
“\N/A”
,则需要检查错误值,而不是检查字符串

替换

For i = rng.Rows.count To 1 Step -1

    'If cell i in the range contains an "N/A", delete cell content
    If rng.Cells(i).Value = "#N/A" Then rng.Cells(i).CellRange.ClearContents (1)
Next

或者,如果要检查所有错误条件(例如
#DIV/0!
),只需使用

Dim cel As Range
For Each cel in rng
    If IsError(cel.Value) Then cel.ClearContents
Next

非常感谢您的回复。因此,我使用您发送的第一个选项替换了代码,但是我得到了一个运行时错误13:这部分的类型不匹配:如果cel.Value=CVErr(xlErrNA),那么cel。ClearContents@jackie-已修复-在我确认单元格中包含错误之前,我不应该对CVErr(xlErrNA)进行测试。非常感谢您的回复!显然,当我用IsNA语句替换代码时,我在代码的这一部分得到了溢出运行时错误“6”:对于I=rng.Rows.count到1步-1@jackie-您需要将
i
声明为
Long
-
行。计数将为
1048576
,大于
整数
可以处理的值。(您还应该注意,您的循环只处理范围内前10%的单元格,
CellRange
不是实际的方法[或者,至少,我从未见过它],而且
ClearContents
不接受参数。)
Dim cel As Range
For Each cel in rng
    If IsError(cel.Value) Then
        If cel.Value = CVErr(xlErrNA) Then cel.ClearContents
    End If
Next
Dim cel As Range
For Each cel in rng
    If IsError(cel.Value) Then cel.ClearContents
Next