我正在尝试为我的老板创建一个简单的Excel报价单,其中必须隐藏不适用的行是某个单元格值= 0,但如果它是= 1,则显示在工作表2中。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("H5")) Is Nothing Then Macro
End Sub
Sub HURows()
BeginRow = 17
EndRow = 34
ChkCol = 4
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
BeginRow = 37
EndRow = 44
ChkCol = 4
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
BeginRow = 48
EndRow = 51
ChkCol = 4
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
End Sub
请您告诉我如何让这个宏自动运行单元格值更改....
答案 0 :(得分:1)
如果Worksheet_Change
sub和If
语句实际用于Macro
宏,那么您需要将我的示例中的If
语句复制到您的语句中,如果它没用过,你可以直接替换它。
它不是最有效的,因为此列中的某些单元格不会隐藏/取消隐藏行,因此可能会调用sub超过所需的次数,但它不应该受到伤害。
以下代码应放在Sheet1
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Sheet1.Columns(4)) Is Nothing Then
' Target Intersects
HURows
Else
' No Intersection
End If
End Sub
此代码应放入新的Module
(而不是工作表或工作簿模块)
Sub HURows()
Dim Sheet As Worksheet
BeginRow = 17
EndRow = 34
ChkCol = 4
Set Sheet = ThisWorkbook.Worksheets("Sheet2")
For RowCnt = BeginRow To EndRow
If Sheet.Cells(RowCnt, ChkCol).Value = 1 Then
Sheet.Cells(RowCnt, ChkCol).EntireRow.Hidden = False
Else
Sheet.Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
BeginRow = 37
EndRow = 44
ChkCol = 4
For RowCnt = BeginRow To EndRow
If Sheet.Cells(RowCnt, ChkCol).Value = 1 Then
Sheet.Cells(RowCnt, ChkCol).EntireRow.Hidden = False
Else
Sheet.Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
BeginRow = 48
EndRow = 51
ChkCol = 4
For RowCnt = BeginRow To EndRow
If Sheet.Cells(RowCnt, ChkCol).Value = 1 Then
Sheet.Cells(RowCnt, ChkCol).EntireRow.Hidden = False
Else
Sheet.Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
End Sub