简体   繁体   中英

Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across another sheet

I'm working on a sheet where we are calculating risk rating and finally selecting questionnaire type which is "Offsite - Lite". By selecting this value, I want certain rows in another sheet name "C. Questionnaire" to be hidden. I am using Column D with Value "Full" in sheet "C. Questionnaire" for this purpose. I want to accomplish this task through a ActiveX button.

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

This is a cleaned up version of your above code, getting rid of Activate and Select :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM