コードを試したところ、次の構文エラーが発生しました。
- myRange が定義されていません。
- 行 (myRange(Row) など) は未定義です。
コードに関するその他の問題:
- myRange は範囲であるため、IsNumeric(myRange) は常に false になります。
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