Excel VBA:单元格值更改时运行宏

时间:2017-10-03 15:46:52

标签: excel vba excel-vba

当单元格值更改时,我无法运行宏。我的代码如下:

这放在工作表的模块中:

Sub HideRow()

    Dim beginRow As Long, endRow As Long, chkCol As Long, chkCommCol As Long
    Dim rowCnt As Long, HiddenRow As Long

beginRow = 1
endRow = Cells(Rows.Count, 1).End(xlUp).Row '<--| set 'endRow' to column A 
last not empty cell row index
chkCol = 1
chkCommCol = 5
HiddenCol = 8

Rows.EntireRow.Hidden = False 'unhides all rows. Subsequent code will hide 
relevant ones

    If Cells(1, chkCommCol).Value = "White Base" Then '<--| if E1 value is 
                  "White Base"
       Cells(HiddenCol, chkCol).EntireRow.Hidden = True
    End If


End Sub

这是放在一般工作簿部分:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = Cells(1, chkCommCol) Then HideRow '<--| if "E1" cell 
           has been changed then run 'HideRow' macro
End Sub

0 个答案:

没有答案