2017-04-25 69 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'並隱藏其他所有內容。 –

相關問題