2016-10-13 116 views
0

我用下面的代碼基於單元格的值隱藏行:顯示行根據單元格的值 - Excel的VBA

Sub HideN() 

Dim RowCnt As Long, uRng As Range 

BeginRow = 8 
EndRow = 232 
ChkCol = 6 

    For RowCnt = BeginRow To EndRow 
     If Cells(RowCnt, ChkCol).Value = 0 Then 
     If uRng Is Nothing Then 
      Set uRng = Cells(RowCnt, ChkCol) 
     Else 
      Set uRng = Union(uRng, Cells(RowCnt, ChkCol)) 
     End If 

     End If 
    Next RowCnt 

If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True 

End Sub 

我有什麼改變,如果我也想取消隱藏行,其中的單元格值是1?

在此先感謝!

MD

回答

0

你可以只添加一個額外的If聲明,NO 4

Sub HideN() 

Dim RowCnt As Long, uRng As Range 

BeginRow = 8 
EndRow = 232 
chkcol = 6 

For RowCnt = BeginRow To EndRow 
    If Cells(RowCnt, chkcol).Value = 0 Then 
     If uRng Is Nothing Then 
      Set uRng = Cells(RowCnt, chkcol) 
     Else 
      Set uRng = Union(uRng, Cells(RowCnt, chkcol)) 
     End If 
    End If 
    If Cells(RowCnt, chkcol).Value = 1 Then ' This is the new line to add 
     Rows(RowCnt).EntireRow.Hidden = False 
    End If 
Next RowCnt 

If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True 

End Sub 
3

這將隱藏所有的0和取消隱藏所有其他。

Sub HideN() 

Dim RowCnt As Long 
Dim BeginRow&, EndRow&, ChkCol& 

BeginRow = 8 
EndRow = 232 
ChkCol = 6 

    For RowCnt = BeginRow To EndRow 
     Rows(RowCnt).Hidden = Cells(RowCnt, ChkCol).Value = 0 
    Next RowCnt 



End Sub 

當然你也可以用過濾器來做同樣的事情。

+1

一個布爾值設置爲布爾表達式的結果的一個很好的例子 - +1 – YowE3K

相關問題