Excel VBA代码(分配给按钮)根据另一个工作表中的单元格值隐藏/取消隐藏行

时间:2018-04-13 17:48:30

标签: excel vba excel-vba

我正在制作一张我们正在计算风险评级的工作表,最后选择问卷类型,即#34; Offsite - Lite"。通过选择此值,我希望另一个工作表名称中的某些行" C.问卷"被隐藏我正在使用D值和#34; Full"在表格中#C; C.问卷"以此目的。我想通过ActiveX按钮完成此任务。

Sub OpenQuestionnaire()

   With ThisWorkbook.Worksheets("C. Questionnaire")
       .Visible = xlSheetVisible
       .Activate

    If Range("XFD3").Value = "Offsite - Lite" Then
          Sheets("C. Questionnaire").Select
          Columns("G").Hidden = True
    ElseIf Range("XFD3").Value = "Onsite - Full" Then
          Columns("G").Hidden = True
    End If

    If Range("XFD1").Value = "No" Then
         Range("H166").Select
         ActiveCell.FormulaR1C1 = "Not Applicable"
         Range("I166").Select
         ActiveCell.FormulaR1C1 = "Not Applicable"
         Range("H166").Select
         Selection.AutoFill Destination:=Range("H166:H181"), Type:=xlFillDefault
         Range("H166:H181").Select
         Selection.AutoFill Destination:=Range("H166:I181"), Type:=xlFillDefault
         Range("H166:I181").Select
         Rows("163:181").EntireRow.Hidden = True
     ElseIf Range("XFD1").Value = "Yes" Then
         Rows("163:181").EntireRow.Hidden = False
     End If

     If Range("XFD2").Value = "No" Then
          Range("H216").Select
          ActiveCell.FormulaR1C1 = "Not Applicable"
          Range("I216").Select
          ActiveCell.FormulaR1C1 = "Not Applicable"
          Range("H216").Select
          Selection.AutoFill Destination:=Range("H216:H232"), Type:=xlFillDefault
          Range("H216:H232").Select
          Selection.AutoFill Destination:=Range("H216:I232"), Type:=xlFillDefault
          Range("H216:I232").Select
          Rows("213:233").EntireRow.Hidden = True
      ElseIf Range("XFD2").Value = "Yes" Then
          Rows("213:233").EntireRow.Hidden = False
      End If

    End With

End Sub

1 个答案:

答案 0 :(得分:0)

这是上述代码的清理版本,删除了ActivateSelect

Sub OpenQuestionnaire()

With ThisWorkbook.Worksheets("C. Questionnaire")
    .Visible = xlSheetVisible

    If .Range("XFD3").Value = "Offsite - Lite" Or .Range("XFD3").Value = "Onsite - Full" Then
          .Columns("G").Hidden = True
    End If

    If .Range("XFD1").Value = "No" Then
         .Range("H166:I181").Value = "Not Applicable"
         .Rows("163:181").EntireRow.Hidden = True
    ElseIf .Range("XFD1").Value = "Yes" Then
         .Rows("163:181").EntireRow.Hidden = False
    End If

    If .Range("XFD2").Value = "No" Then
        .Range("H216:I232").Value = "Not Applicable"
        .Rows("213:233").EntireRow.Hidden = True
    ElseIf Range("XFD2").Value = "Yes" Then
        .Rows("213:233").EntireRow.Hidden = False
    End If

End With

End Sub