与上一个问题一致,我问我试图隐藏包含零的行,但跳过包含任何一个或多个单元格中任何文本的行。我以前在跳过空行时得到了很好的帮助,现在希望得到更多的帮助。我已经在我能找到的每个论坛上搜索了互联网,并且没有发现任何我需要的东西。该隐藏有两个代码,并且该隐藏代码的精确副本但隐藏设置为false。这是隐藏的那个。
Sub HideRows()
Dim R As Long
Dim Rng As Range
If Selection.Rows.Count > 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange
End If
For R = 1 To Rng.Rows.Count
Set myRange = Range(Rng(R, 2), Rng(R, Rng.Columns.Count))
If Application.CountBlank(myRange) <> myRange.Cells.Count And IsNumeric(myRange(Row)) = False Then
If Application.Sum(myRange) = 0 Then
Rng.Rows(R).Hidden = True
End If
End If
Next R
End Sub
顺便说一下,我知道IsNumeric(myRange(Row)) = False
确实应该是= True
,但出于某种原因,我的一个工作表可以使用此设置,如果我改为True,它几乎什么都不做。< / p>
在预先感谢您的帮助。
答案 0 :(得分:3)
当我尝试使用您的代码时,出现以下语法错误:
您的代码存在其他问题:
If Application.CountBlank(myRange) <> myRange.Cells.Count
表示不会隐藏空行。IsNumeric和IsNumber都在单个值上运行。我在文档中找不到任何建议可以使它们在阵列,集合或范围上运行。我的实验已经产生了与此相符的结果。我不相信有任何方法可以处理困难的情况,除非通过检查一行中的单个细胞。
我想我已经针对所有边界条件测试了以下代码,但我无法保证这一点。它隐藏了只包含零的空白行和行。如果选择范围,则该范围之外的列将被视为空白。
Sub HideRows()
Dim ColCrnt As Integer
Dim Hide As Boolean
Dim myRange As Range
Dim R As Long
Dim Rng As Range
If Selection.Rows.Count > 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange
End If
For R = 1 To Rng.Rows.Count
Set myRange = Range(Rng(R, 1), Rng(R, Rng.Columns.Count))
If Application.CountBlank(myRange) = myRange.Cells.Count Then
' Blank row
Hide = True
ElseIf Application.Sum(myRange) <> 0 Then
' At least on numeric cell with a non-zero value
Hide = False
Else
' Row contains one or more cells containing text, booleans or zeroes
' Hide if all these cells are zeros.
ColCrnt = Rng.Columns.Count
Set myRange = Rng(R, ColCrnt)
If IsCellZero(myRange) Or IsEmpty(myRange) Then
' Last cell of row is zero or blank so will have to check row
Do While True
' Skip to first non-blank cell to left or column 1
' if no non-blank cells
Set myRange = myRange.End(xlToLeft)
If myRange.Column < Rng(R, 1).Column Then
' Have move outside selection
Hide = True
Exit Do
End If
If myRange.Column = Rng(R, 1).Column Then
' Have reached column 1
If IsCellZero(myRange) Or IsEmpty(myRange) Then
' Column 1 is zero or blank so nothing interesting on row
Hide = True
Exit Do
Else
' Column 1 is not zero or blank
Hide = False
Exit Do
End If
End If
If Not IsCellZero(myRange) Then
Hide = False
Exit Do
End If
If myRange.Column = Rng(R, 1).Column Then
' No non-zero cells found
Hide = True
Exit Do
End If
Loop
Else
' Last cell of row is neither zero nor empty
Hide = False
End If
End If
If Hide Then
Rng.Rows(R).Hidden = True
Else
Rng.Rows(R).Hidden = False
End If
Next R
End Sub
Function IsCellZero(Rng As Range) As Boolean
' Rng must be a single cell. Returns true only if Rng.Value is numeric zero
' Function uses IsNumber because IsNumeric returns True
' for empty cells and booleans
If Application.WorksheetFunction.IsNumber(Rng.Value) Then
If Val(Rng.Value) = 0 Then
IsCellZero = True
Else
IsCellZero = False
End If
Else
' Value is blank, text or boolean
IsCellZero = False
End If
End Function
答案 1 :(得分:2)
问题的原因是And IsNumeric(myRange(Row)) = False
0
。因此(因为myRange是从第2列开始定义的)myRange(Row)
是指行A
所指的myRange
列中的单个单元格。(Row)
位,IsNumeric
将始终返回FALSE
此外,Set myRange = Range(Rng(R, 2),
指的是使用范围的行R,向右偏移一列
结论:
If Application.Count(myRange) > 0 And _
Application.CountBlank(myRange) + _
Application.Count(myRange) = myRange.Cells.Count Then
顺便说一下,好的做法DIM
所有变数。这可以确定(Row)
的问题。如果您将Option Explicit
添加到模块的顶部,这将成为强制性目标。