我正在尝试做一些简单的操作,如果该行中的所有数据都为空,则隐藏一行。
我已经搜索过,无法让它发挥作用。这就是我到目前为止所做的。
Sub UpdateFields_630()
Application.ScreenUpdating = False
Dim sht3 As Worksheet
Set sht3 = ThisWorkbook.Worksheets("630 BOM")
Dim LastRow As Long, LastCol As Long
Dim rng As Range, c As Range
On Error GoTo 0
With sht3
Set rng = Cells
LastRow = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
LastCol = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
For Each c In Range(Cells(9, "E"), Cells(LastRow, LastCol))
If c.Value = "" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next
End With
sht3.Protect
Set rng = Nothing
Set sht3 = Nothing
Application.ScreenUpdating = True
End Sub
排序前
排序后
第13,14,19,20和38行因某些原因隐藏在此代码中,无法弄清楚原因。
如果我基于column "A" total = 0
隐藏,我可以让它工作,但行27 & 30
会隐藏。我已经尝试了If c.Value = "x" Then
c.EntireRow.Hidden = False
,但似乎没有做任何事情。
感谢您的帮助。
答案 0 :(得分:1)
1-如果任何的单元格为空,您将隐藏该行,而不是所有为空
2-您没有对您的范围进行限定,使您的with子句无用。
您可以使用Application.CountA
检查范围内的所有单元格是否为空。将其应用于每一行以确定是否应隐藏它。
' v v (notice these dots)
For Each c In .Range("E9", .Cells(LastRow, LastCol)).Rows
c.EntireRow.Hidden = Application.CountA(c) = 0
Next
修改强>
由于空白单元格有公式,CountA
不会计算为空白。因此请改用:
For Each c In .Range("E9", .Cells(LastRow, LastCol)).Rows
c.EntireRow.Hidden = Application.CountIf(c, "") = c.Cells.Count
Next