2015-05-15 127 views
-1

我一直在嘗試運行下面的代碼,但每當我在列I中將值更改爲0時,什麼都不會發生。Excel事件更改:隱藏或取消隱藏行

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 

Dim wb_afskrivninger As Workbook 
Dim ws_afskrivningsberegning As Worksheet 

Set wb_afskrivninger = ActiveWorkbook 
Set ws_afskrivningsberegning = ThisWorkbook.Sheets("afskrivningsberegning") 

Dim columnI_rowcount As Long 

columnI_rowcount = ws_afskrivningsberegning.Range("I" & Rows.Count).End(xlUp).Row 

Dim columnI_0count As Long 

Application.ScreenUpdating = False 

For columnI_0count = 0 To column_rowcount 

    If ws_afskrivningsberegning.Range("I" & columnI_rowcount) = 0 Then 
     Rows(columnI_rowcount).EntireRow.Hidden = True 
     Else: Rows(columnI_rowcount).EntireRow.Hidden = False 
    End If 

Next columnI_0count 

Application.ScreenUpdating = True 

End Sub 

幫助,將不勝感激。

在此先感謝。

+0

爲什麼'Activeworkbook'更換MSGBOX行要在目標變爲0

Private Sub Worksheet_Change(ByVal Target As Range) If Target.Count > 1 Then Exit Sub If Target.Column = 9 Then ' this is column "I" If Target = 0 Then MsgBox "You have changed the target value to Zero" End If End If End Sub 

使用任何代碼?這個代碼放在哪裏?工作表在哪裏'afskrivningsberegning'您需要使用'Intersect'。 [這](http://stackoverflow.com/questions/13860894/ms-excel-crashes-when-vba-code-runs/13861640#13861640)會讓你開始。 –

+0

好的,謝謝。代碼放在我工作的工作簿中,所以我想不需要使用Activeworkbook。 –

+0

你不能用你的代碼「取消隱藏」一行,如果它已經隱藏,代碼將無法看到它。查看我的示例,在循環遍歷範圍之前先取消隱藏行。 – Davesexcel

回答

0

將它放在工作表模塊中,右鍵單擊工作表選項卡並選擇查看代碼,複製並粘貼此代碼。如果你正在尋找一個代碼隱藏該行= 0那就試試這個

Sub HideZeros() 
    Dim Rws As Long, rng As Range, c As Range 

    Columns("I:I").EntireRow.Hidden = False 

    Rws = Cells(Rows.Count, "I").End(xlUp).Row 
    Set rng = Range(Cells(1, "I"), Cells(Rws, "I")).SpecialCells(xlCellTypeConstants) 

    For Each c In rng.Cells 
     If c.Value = 0 Then c.EntireRow.Hidden = True 
    Next c 

End Sub