提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何通过单击Excel中的特定单元格来触发或运行宏?

在使用Microsoft Excel时,您可能会知道如何使用命令按钮来运行某个宏。 但是,您是否知道如何通过单击工作表中的特定单元来运行宏? 本文将向您展示通过单击详细信息中的特定单元格来触发宏的方法。

通过点击具有VBA代码的特定单元格来触发或运行宏


将多个工作表/工作簿合并到一个工作表/工作簿中:

将多个工作表或工作簿合并到一个工作表或工作簿中可能是您​​日常工作中的一项重大任务。 但是,如果你有 Kutools for Excel,其强大的效用 - 结合 可以帮助您将多个工作表,工作簿快速合并到一个工作表或工作簿中。

Kutools for Excel 包含了比300更方便的Excel工具。 免费试用60天无限制。 立即下载免费试用版

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

通过点击具有VBA代码的特定单元格来触发或运行宏


以下VBA代码可以帮助您通过单击Excel中的特定单元来运行宏。 请做如下。

1。 在单元的工作表上,您需要单击以运行宏,右键单击工作表选项卡,然后单击 查看代码 从上下文菜单。

2。 在里面 Microsoft Visual Basic for Applications 窗口中,将下面的VBA脚本复制并粘贴到代码窗口中。

VBA代码:通过单击特定单元触发或运行宏

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("D4")) Is Nothing Then
            Call MyMacro
        End If
    End If
End Sub

笔记:

1。 在代码中,D4是您要单击以运行宏的单元格;

2。 请替换代码名称 MyMacro 与您将运行在工作表中的宏。 看截图:

3。 按 其他 + Q 键同时关闭 Microsoft Visual Basic for Applications 窗口。

从现在开始,在当前工作表中单击单元格D4时,将立即触发指定的宏。


相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    M. Symonds · 1 months ago
    Thanks for this code. Is it possible to get this to work by clicking on a cell that is merged with others?
  • To post as a guest, your comment is unpublished.
    Shahrokh · 3 months ago
    Thanks, but how about several cells for clicking for running several macros.
    • To post as a guest, your comment is unpublished.
      crystal · 2 months ago
      Hi,
      Try the below VBA code.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim xRgArr As Variant
      Dim xFunArr As Variant
      Dim xFNum As Integer
      Dim xStr As String
      Dim xRg As Range
      xRgArr = Array("A1", "D1", "C1") 'Cells used to trigger macro
      xFunArr = Array("Code name1", "Code name2", "Code name3") 'The corresponding code names
      If Selection.Count = 1 Then
      For xFNum = 0 To UBound(xRgArr)
      Set xRg = ActiveSheet.Range(xRgArr(xFNum))
      If Not Intersect(Target, xRg) Is Nothing Then
      xStr = xFunArr(xFNum)
      Application.Run xStr
      End If
      Next
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    adam · 8 months ago
    Why not just use some newer version it will be more easy
    https://games.lol/racing/
  • To post as a guest, your comment is unpublished.
    Roger · 8 months ago
    Excel 2002 (XP): If a workbook is opened by selecting "File" and pressing "Shift" it disables macros in that sheet, the problem is that if I select "Tools > Macro > Macros..." I can run the macro anyway, how to solve?
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Roger,
      We haven't tested the code in Excel 2002(XP). Why not use the newer version of Microsoft Office? It will be more easy for your work.
  • To post as a guest, your comment is unpublished.
    James Clements · 9 months ago
    My full code in this macro is:


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim val As String
    REM val = Range("A2").Value

    If Selection.Count = 1 Then
    If Not Intersect(Target, Range("D24")) Is Nothing Then
    REM Call MyMacro
    val = Range("D24").Value
    Range("B27").Value = val
    End If
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      goalken · 1 months ago
      This topic is very interesting and I am interested but do not know where to find, thankfully you create this topic, hope everyone will help me http://run-3.online
  • To post as a guest, your comment is unpublished.
    James Clements · 9 months ago
    i'm using OpenOffice and have right clicked on sheet-tab and selected events and then selected this macro from MyMacros.. However I get and error on the following line: If Selection.Count = 1 Then >>> "Basic runtime error, variable not defined...
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi James,
      The code only works for Microsoft Office Excel. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Alber · 10 months ago
    Thanks but what about merged cells?
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi Alber,
      The code does not work for merged cells.
  • To post as a guest, your comment is unpublished.
    Phil H · 10 months ago
    Hi.

    I have this working well, but would like to add a condition to running the macro. I only want to run the macro if the cell alongside the cell I click into contains a certain value.
    e.g. When I click on cell F6, I want the macro to run if cell E6 contains "x", but if cell E6 is blank, the macro must not run.
    Hope that makes sense.
    Thanks

    here is my original code without the condition:


    Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
    If Not Intersect(Target, Range("F6:F18")) Is Nothing Then
    Call datePick
    End If
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      crystal · 9 months ago
      Hi,
      The following VBA code can help you solve the problem. Please have a try and thank you for your comment.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim xRg As Range
      If Not Intersect(Target, Range("F6:F18")) Is Nothing Then
      Set xRg = ActiveSheet.Cells(Target.Row, Target.Column - 1)
      If (xRg.Value = "") Or (xRg.Value <> "X") Then Exit Sub
      Call datepick
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Medical Coder · 1 years ago
    Great. Its working well... Thank you....
  • To post as a guest, your comment is unpublished.
    Wayne · 1 years ago
    This worked perfectly and will save me a load of time - thank you for sharing your knowledge - much appreciated!
  • To post as a guest, your comment is unpublished.
    Simon Jones · 1 years ago
    If you want to have multiple macros, cells running different macros on the same page - is that possible?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Simon,
      The below VBA script can help you to run different macros by clicking on cells on the same page.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Count = 1 Then
      If Not Intersect(Target, Range("D4")) Is Nothing Then Call MyMacro1
      If Not Intersect(Target, Range("D8")) Is Nothing Then Call MyMacro2
      If Not Intersect(Target, Range("D10")) Is Nothing Then Call MyMacro3
      End If
      End Sub

      Please add line "If Not Intersect(Target, Range("D10")) Is Nothing Then Call MyMacro" to run more macro by clicking cell. And change the cell and macro names in the code based on your needs.
      • To post as a guest, your comment is unpublished.
        Camila · 8 months ago
        It isn't working on my Excel. The code is correct?
        • To post as a guest, your comment is unpublished.
          crystal · 2 months ago
          Hi Camila,
          Sorry for the inconvenience. Try the below VBA code.

          Private Sub Worksheet_SelectionChange(ByVal Target As Range)
          Dim xRgArr As Variant
          Dim xFunArr As Variant
          Dim xFNum As Integer
          Dim xStr As String
          Dim xRg As Range
          xRgArr = Array("A1", "D1", "C1") 'Cells used to trigger macro
          xFunArr = Array("Code name1", "Code name2", "Code name3") 'The corresponding code names
          If Selection.Count = 1 Then
          For xFNum = 0 To UBound(xRgArr)
          Set xRg = ActiveSheet.Range(xRgArr(xFNum))
          If Not Intersect(Target, xRg) Is Nothing Then
          xStr = xFunArr(xFNum)
          Application.Run xStr
          End If
          Next
          End If
          End Sub
  • To post as a guest, your comment is unpublished.
    Matt · 2 years ago
    Trigger Or Run A Macro By Clicking A Specific Cell With VBA Code, couldn't get this to work. I tried it many different way, but it never acheived what was promised.
    • To post as a guest, your comment is unpublished.
      Peter · 2 years ago
      This code will only work if you place it in the "ThisWorkbook" module. It does not work on a general module.
      workbookname > Microsft Excel Objects > ThisWorkbook.
      • To post as a guest, your comment is unpublished.
        PritishS · 1 years ago
        This code will work on worksheet code module. You need to right click on sheet tab and click on 'View Code'. This will open code module for that worksheet only. Then paste the mentioned code.