VBA: how to hide rows if a cell contains a certain text
I am using Excel 2013. I am new to VBA. I found a code that would simply hide 2 rows (36 and 37) if my cell N39 is equal to "Passed"
I found that code but I receive the message "ambiguous name detected "Worksheet_Change"
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("N39").Value = "Passed" Then
Rows("36:37").EntireRow.Hidden = True
ElseIf Range("N39").Value = "Failed" Then
Rows("36:37").EntireRow.Hidden = False
End If
End Sub
so I tried the name of my worksheet but it does nothing
Private Sub NRF(ByVal Target As Range)
If Range("N39").Value = "Passed" Then
Rows("36:37").EntireRow.Hidden = True
ElseIf Range("N39").Value = "Failed" Then
Rows("36:37").EntireRow.Hidden = False
End If
End Sub
Could it be because I have another code above?
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim celltxt As String
celltxt = ActiveSheet.Range("E39").Text
If InStr(1, celltxt, "P670-Staffing") Then
MsgBox "Add the job title and type of hire in the description cell - column H" & vbNewLine & vbNewLine & "If this is a new position, please obtain your HRBP's approval"
End If
End Sub
Any idea of what I am doing wrong?
Solution 1:
Any idea of what I am doing wrong?
I found that code but I receive the message "ambiguous name detected "Worksheet_Change"
You can't have two functions with the same name and parameters Worksheet_Change
.
I removed the previous code and it works. How could it make it work with the other code?
You need to put all of the code inside a single Worksheet_Change
function.