Home » excel » excel – Multiple Worksheet_Change Issue

excel – Multiple Worksheet_Change Issue

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to have multiple Worksheet_Change in one sheet, however I’m having issues implementing them.

I tried:

Private Sub Worksheet_Change(ByVal Target As Range)

  If Target.Address = "$B$6" Then
    Select Case Target.Value
      Case "Yes"
        Range("DataYes").EntireRow.Hidden = True
        Range("DataNo").EntireRow.Hidden = False
      Case "No"
        Range("DataYes").EntireRow.Hidden = False
        Range("DataNo").EntireRow.Hidden = True
      Case ""
        Range("DataYes").EntireRow.Hidden = True
        Range("DataNo").EntireRow.Hidden = True
    End Select
  End If


  If Target.Address = "$Q$6" Then
    Select Case Target.Value
      Case "TRUE"
        Range("DataNo").EntireRow.Hidden = False
        Range("CrComments").EntireRow.Hidden = False
      Case "FALSE"
        Range("CrComments").EntireRow.Hidden = True
    End Select
  End If

End Sub

In This case, only the first piece of code works. The second doesn’t. No error messages.

I also tried:

Private Sub Worksheet_Change(ByVal Target As Range)

Select Case Target.Address

  Case "$B$6"
        If Range("B6").Value = "Yes" Then
            Range("DataYes").EntireRow.Hidden = True
            Range("DataNo").EntireRow.Hidden = False
        ElseIf Range("B6").Value = "No" Then
            Range("DataYes").EntireRow.Hidden = False
            Range("DataNo").EntireRow.Hidden = True
        ElseIf Range("B6").Value = "" Then
            Range("DataYes").EntireRow.Hidden = True
            Range("DataNo").EntireRow.Hidden = True
        End If
      End If

  Case "$Q$6"
        If Range("Q6").Value = "TRUE" Then
            Range("CrComments").EntireRow.Hidden = False
        ElseIf Range("Q6").Value = "FALSE" Then
            Range("CrComments").EntireRow.Hidden = True
        End If
      End If


End Select
End Sub

In This code, I got a End if without block if error for the last End If of the first case.

Deleting the errored End IFs still doesn’t make the second piece of code work

How to&Answers:

Cell values of “FALSE” and “TRUE” will be read as False and True (boolean values, not strings), so your test on Q6 needs to be changed.

If Target.Address = "$Q$6" Then
    Select Case Target.Value
      Case True
        Range("DataNo").EntireRow.Hidden = False
        Range("CrComments").EntireRow.Hidden = False
      Case False
        Range("CrComments").EntireRow.Hidden = True
    End Select
End If

Answer:

You have two wrong “End If”, here is the fixed code:

Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Address

      Case "$B$6"
            If Range("B6").Value = "Yes" Then
                Range("DataYes").EntireRow.Hidden = True
                Range("DataNo").EntireRow.Hidden = False
            ElseIf Range("B6").Value = "No" Then
                Range("DataYes").EntireRow.Hidden = False
                Range("DataNo").EntireRow.Hidden = True
            ElseIf Range("B6").Value = "" Then
                Range("DataYes").EntireRow.Hidden = True
                Range("DataNo").EntireRow.Hidden = True
            End If

      Case "$Q$6"
            If Range("Q6").Value = "TRUE" Then
                Range("CrComments").EntireRow.Hidden = False
            ElseIf Range("Q6").Value = "FALSE" Then
                Range("CrComments").EntireRow.Hidden = True
            End If
    End Select
End Sub

As for you original issue it should not change anything.

Could you provide more information please?