excel隐藏范围内的空单元格

我试图做一些简单的事情,如果该行中的所有数据都是空的,就隐藏一行。

我已经search,不能得到它的工作。 这是我迄今为止。

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 

Sort之前

之前

sorting后

后

由于某种原因,第13,14,19,20和38行被隐藏着这段代码,并且不知道为什么。

我可以得到这个工作,如果我隐藏基于column "A" total = 0但行27 & 30行将隐藏。 我试过If c.Value = "x" Then c.EntireRow.Hidden = False ,这似乎没有做任何事情。

谢谢你的帮助。

1-如果任何单元格为空,则隐藏该行,而不是全部为空

2-你不符合你的范围,使你的子句无用。

您可以使用Application.CountA来检查范围内的所有单元格是否为空。 将其应用于每一行以决定是否应该隐藏。

  ' vv (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