问题
Im working on a vba project where i need to check a range (e.g. A1:Z100), and hide all rows where any cell in a checked row range has the value '0'.
For example
Row 1 - hide: 0 0 0 0 0 0 0 0 0 ... ' as only zero values
Row 2 - don't hide: 0 0 0 0 1 0 0 1 0 ... ' as also other value(s)
Can anyone help me out with this?
回答1:
For your query try below sub.
Sub HideZeoRows()
Dim rng As Range
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each rng In Range("A1:A" & lRow)
If Application.CountIf(Range("A" & rng.Row & ":Z" & rng.Row), 0) = 26 Then
'26 for column z
rng.EntireRow.Hidden = True
End If
Next rng
End Sub
回答2:
Try this code (necessary comments in code)
Sub Main
Dim lastRow As Long, i As Long
' Get last row
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastRow
If CheckRow(i) Then Rows(i).Hide
Next
End Sub
' Function used to dtermine if row contains only 0s or empty cells.
' If so, return true.
Function CheckRow(rowNumber as Long) as Boolean
CheckRow = True
Dim lastCell As Long, i As Long
' Find last cell in a row
lastCell = Cells(rowNumber, Columns.Count).End(xlToLeft).Column
For i = 1 To lastCell
If Not IsEmpty(Cells(rowNumber, i)) And Cells(rowNumber, i) <> 0 Then
CheckRow = False
Exit Function
End If
Next
End Function
回答3:
Hide Zero String Rows
- Hides each worksheet row where all cells in the same row of a given range (literally) contain a zero
0(string or value). - If you want to include empty and blank cells (consider them as zeros) then replace both occurrences of
rng.Textwithrng.Valueand replace"0"with0.
The Code
Sub TESThideZeroStringRows()
Dim rng As Range: Set rng = Range("A1:J10")
hideZeroStringRows rng
End Sub
Sub hideZeroStringRows(rng As Range)
If Not rng Is Nothing Then
Dim hRng As Range ' Hide Range
Dim rRng As Range ' Current Row Range
Dim Data As Variant
If rng.Cells.CountLarge > 1 Then
Data = rng.Text
Else
ReDim Data(1 To 1, 1 To 1)
Data(1, 1) = rng.Text
End If
Dim cCount As Long: cCount = UBound(Data, 2)
Dim rOffset As Long: rOffset = rng.Row - 1
Dim i As Long
Dim j As Long
Dim doNotHide As Boolean
For i = 1 To UBound(Data, 1)
For j = 1 To cCount
If Data(i, j) <> "0" Then
doNotHide = True
Exit For
End If
Next j
If doNotHide Then
doNotHide = False
Else
buildRange hRng, rng.Cells(i + rOffset, 1)
End If
Next i
Application.ScreenUpdating = False
rng.EntireRow.Hidden = False
If Not hRng Is Nothing Then
hRng.EntireRow.Hidden = True
End If
Application.ScreenUpdating = True
End If
End Sub
Sub buildRange( _
ByRef BuiltRange As Range, _
AddRange As Range)
If BuiltRange Is Nothing Then
Set BuiltRange = AddRange
Else
Set BuiltRange = Union(BuiltRange, AddRange)
End If
End Sub
Hide Zero Rows
- Hides each worksheet row where the sum of the cells in the same row of a given range is equal to
0.
The Code
Sub TESThideZeroRows()
Dim rng As Range: Set rng = Range("A1:J10")
hideZeroRows rng
End Sub
Sub hideZeroRows(rng As Range)
If Not rng Is Nothing Then
Dim hRng As Range ' Hide Range
Dim rRng As Range ' Current Row Range
For Each rRng In rng.Rows
If Application.Sum(rRng) = 0 Then
If hRng Is Nothing Then
Set hRng = rRng
Else
Set hRng = Union(hRng, rRng)
End If
End If
Next rRng
Application.ScreenUpdating = False
rng.EntireRow.Hidden = False
If Not hRng Is Nothing Then
hRng.EntireRow.Hidden = True
End If
Application.ScreenUpdating = True
End If
End Sub
来源:https://stackoverflow.com/questions/65734209/vba-hide-all-rows-in-range-if-all-values-in-row-are-0