0

我正在尝试为我的老板创建一个简单的 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

请你告诉我如何让这个宏在单元格值更改时自动运行....

4

1 回答 1

1

如果您的Worksheet_Change子和If语句实际上用于Macro宏,那么您需要将If我的示例中的语句复制到您的示例中,如果它没有使用,那么您可以替换它。

它不是最有效的,因为此列中的某些单元格不会隐藏/取消隐藏行,因此子程序的调用次数可能比需要的次数多,但应该不会造成伤害。

以下代码应放入工作表代码中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
于 2013-03-04T13:53:29.723 回答