如何在 VBA 中以编程方式在某些工作表单元格数据旁边添加按钮? [英] How to add a button programmatically in VBA next to some sheet cell data?

查看:35
本文介绍了如何在 VBA 中以编程方式在某些工作表单元格数据旁边添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以生成 100 行(和 2 列)的数据.对于每一行(在第 3 列中),我需要添加一个按钮,单击该按钮时,会显示一个自定义模式对话框,为用户提供 4 个选项/按钮可供选择.

I have a function that generates data for say 100 rows (and 2 columns). For each row (in the 3rd column) I need to add a button which, when clicked, brings up a custom modal dialog box giving the user 4 options/buttons to choose from.

知道怎么做吗?

推荐答案

我认为这足以让您走上一条不错的道路:

I think this is enough to get you on a nice path:

Sub a()
  Dim btn As Button
  Application.ScreenUpdating = False
  ActiveSheet.Buttons.Delete
  Dim t As Range
  For i = 2 To 6 Step 2
    Set t = ActiveSheet.Range(Cells(i, 3), Cells(i, 3))
    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
    With btn
      .OnAction = "btnS"
      .Caption = "Btn " & i
      .Name = "Btn" & i
    End With
  Next i
  Application.ScreenUpdating = True
End Sub

Sub btnS()
 MsgBox Application.Caller
End Sub

它创建按钮并将它们绑定到butnS().在 btnS() 子中,您应该显示对话框等.

It creates the buttons and binds them to butnS(). In the btnS() sub, you should show your dialog, etc.

这篇关于如何在 VBA 中以编程方式在某些工作表单元格数据旁边添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆