将Enter键按在特定的单元格上时,如何运行宏



我当前正在尝试创建一个工具,该工具将在电池B4上按Enter键时调用/运行另一个宏/子。

单元格B4将是搜索字段。每当有人在此字段中键入某些内容并在其键盘上输入某些内容时,我都希望另一个称为" searchContains"的宏来运行

我尝试使用此代码,但我不确定如何专门定义B3单元格:

application.onkey"〜"," searchContains"

您也可以尝试此代码:1.打开VBA编辑器2.转到工作表VBA编辑器3.选择工作表和更改活动4.复制代码

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 4 And Target.Column = 2 Then
    'The function will be called only if the cell has a value
    If ActiveSheet.Cells(4, 2).Value <> "" Then
       'Call the function here
       Call sample_function(ActiveSheet.Cells(4, 2).Value)
    End If
End If
End Sub

Sub sample_function(input_String As String)
MsgBox "You input: " & input_String, vbInformation
End Sub

最新更新