如果#REF!然后隐藏某些行

时间:2016-06-29 08:44:44

标签: excel vba if-statement

我已经创建了两张,没有。 1很好地显示了数据,另一个导入了数据。因此,没有。 1从表单中获取数据。 2.但是有时表2中没有数据。这给表1中的单元格“#REF!”。发生这种情况时,我需要隐藏工作表1中的某些行。

所以我想象这样的事情

Sub HideCharts()

BeginRow = 132
EndRow = 138
ChkCol = 8

For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = 0 Then
        Cells(RowCnt, ChkCol).EntireRow.Hidden = True
    End If
Next RowCnt

        For RowCnt = BeginRow To EndRow
            If Cells(RowCnt, ChkCol).Value <> 0 Then
                Cells(RowCnt, ChkCol).EntireRow.Hidden = False
            End If
        Next RowCnt
End Sub 

然而,这仅在数据为0时有效且我不知道如何捕获#REF!这也只隐藏包含0值的行,但我需要捕获某些行,如130到140

所以我想,除了Stack Overflow还有什么可以转向?

3 个答案:

答案 0 :(得分:2)

尝试使用&#34; IsError&#34;在你的if语句中它应该有用。

For RowCnt = BeginRow To EndRow
    If IsError(Cells(RowCnt, ChkCol)) Then
        Cells(RowCnt, ChkCol).EntireRow.Hidden = True
    End If
Next RowCnt

答案 1 :(得分:1)

像这样:

If Cells(RowCnt, ChkCol).Value = 0 Then更改为If Cells(RowCnt, ChkCol).Value = 0 or iserror(Cells(RowCnt, ChkCol).Value)

Sub HideCharts()

BeginRow = 132
EndRow = 138
ChkCol = 8

For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = 0 or iserror(Cells(RowCnt, ChkCol).Value) Then
        Cells(RowCnt, ChkCol).EntireRow.Hidden = True
    Else  
        'You don't need this if all the rows are visible at start  
        Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    End If
End Sub 

答案 2 :(得分:1)

您可以使用<h1>函数捕获单元格值错误。

如果您只想要特定的单元格值错误(例如,因为您不想隐藏不是由丢失数据而是由于数据损坏而导致的错误),请使用IsError功能。有关可能的错误值列表,请参阅this。在你的情况下,它将是

CVErr