2017-04-25 156 views
0

我想做一些简单的事情,并隐藏一行,如果该行中的所有数据都是空的。excel隐藏范围内的空单元格

我已经搜索,无法得到它的工作。这是我迄今为止。

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 

之前排序

before

后排序

after

行13,14,19,20和38因为某种原因被隐藏了这段代码,并且找不到原因。

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

感谢您的任何帮助。

+0

相反检查如果单元格值是“”,检查单元格的值的长度为0 –

+0

@RichHolton我会怎么做呢?之前没有在VBA中使用过'LEN'。 Thx –

回答

1

1-你躲在如果其细胞的任何是空的,如果没有他们所有是空行

2 - 你不配你的范围,使您与条款没用。

您可以使用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 

EDIT

由于空白细胞具有式,​​然后不会计数为空白。出于这个原因,使用这个代替:

For Each c In .Range("E9", .Cells(LastRow, LastCol)).Rows 
     c.EntireRow.Hidden = Application.CountIf(c, "") = c.Cells.Count 
    Next 
+0

即时通讯还是很新的VBA,仍然在学习。感谢您的意见。您的代码应用于我的循环不会执行任何操作。我想我错过了一些东西。 –

+0

@MattTaylor通过调试器遍历它,检查'lastRow'和'lastCol'的calues。也可以使用范围:'Set rng = .Cells'(单元格之前的点,意为'sht3.Cells')。 –

+0

@ASH我在这里张贴之前已经多次穿过它。 'lastrow&lastcol'的选择范围选择它应该达到的整个范围。它似乎只是检查数据的'lastcol'并隐藏其他所有内容。 –

相关问题