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

我已经创build了2张纸, 1很好地显示数据,另一个input数据。 所以表号 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,还有谁能够转向?

喜欢这个:

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 

尝试在你的if语句中使用“IsError”。

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

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

如果您只想要特定的单元格值错误(例如,因为您不想隐藏不是由于缺less数据而是由于数据损坏而导致的错误),请使用CVErr函数。 看到这个可能的错误值的列表。 在你的情况下,将是

 If CVErr(Cells(...)) = xlErrRef Then