Execute a subroutine when a user enters a trigger into a cell

前端 未结 2 1697
礼貌的吻别
礼貌的吻别 2021-01-27 17:19

Example data in Excel:

   A    B    C  
1  9         5  
2  4    y    3  
3  1         9  
4  66        4  
5  5         9  

What I want to d

2条回答
  •  一个人的身影
    2021-01-27 17:54

    As siddarth suggested, Worksheet_change() is what you are looking for. Here is how you may achieve your task without any leaks. Paste this code inside the sheet in which you are editing the y value.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim MyRng As Range
    Set MyRng = Range("B:B")
    
    Dim PieRng As Range
    'Intersect will ensure your current cell lies on column B
    Set PieRng = Intersect(Target, MyRng)
    
    'if conditions to ensure trigger code only one cell edited on Col B and is 'y/Y'.
    If Target.Columns.Count = 1 And Target.Rows.Count = 1 Then
        If Not PieRng Is Nothing And LCase(Target.Text) = "y" Then
           'Do my stuff here when y / Y are entered in Column B of current sheet
           MsgBox "You entered " & Target.Value & " in Col B"
        End If
    End If
    
    
    End Sub
    

    Let us know if it fails...

提交回复
热议问题