在单元格中放置命令按钮 MS Excel vba

发布于 2024-12-18 20:15:28 字数 192 浏览 0 评论 0原文

我想通过 VBA 代码在单元格中放置一个命令按钮。说位置B3。我为此目的使用了宏记录器,但它为我提供了按钮的顶部底部值。我不希望这样,因为如果我将代码带到具有其他屏幕分辨率的其他计算机上,代码将会失败。单元格位置(示例 B3)将是绝对位置。

你能建议我一种方法来做到这一点吗?

PS 它是一个 activeX 按钮

谢谢

I want to place a command button in a cell though VBA code. Say position B3. I used macro recorder for this purpose but it gives me the top bottom values of the button. I don't want that cuz if I take my code to some other computer with another screen resolution, the code will fail. A cell position (example B3) will be an absolute position.

Can you suggest me a way to do this.

P.S Its an activeX button

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜扑 2024-12-25 20:15:28

您不能将任何对象放置在单元格“中”,只能放置在单元格上方。您可以将按钮的 Left 和 Top 属性设置为单元格的 Left/Top。

Sub Tester()
    Dim rng As Range
    Set rng = ActiveSheet.Range("B3")
    With ActiveSheet.OLEObjects("CommandButton1")
        .Top = rng.Top
        .Left = rng.Left
        .Width = rng.Width
        .Height = rng.RowHeight
    End With
End Sub

You can't place any object "in" a cell, only over it. You can set the button's Left and Top properties to the Cell's Left/Top.

Sub Tester()
    Dim rng As Range
    Set rng = ActiveSheet.Range("B3")
    With ActiveSheet.OLEObjects("CommandButton1")
        .Top = rng.Top
        .Left = rng.Left
        .Width = rng.Width
        .Height = rng.RowHeight
    End With
End Sub
酷遇一生 2024-12-25 20:15:28

“在单元格中放置按钮”的替代方案

使用工作表事件直接选择单元格执行代码:Worksheet_SelectionChange。将代码放置在特定工作表的模块中。根据需要对单元格进行颜色/边框/文本设置。任何计算机或屏幕上的单元都是单元。当用户单击简短的标签/描述时,我使用它来引用加载到用户表单中的帮助,从帮助表上查找。使用单元格/按钮可避免 IT 部门对 Active-X 对象的投诉。

需要考虑的事项,示例代码如下:

  1. Target.Address 使用“$”字符返回绝对地址
  2. 在代码中使用Select Case,即使您只有一个单元格/按钮。这简化了添加单元格/按钮的路径,稍后
  3. 考虑在电子表格上使用命名范围,并在代码中引用它们。这样,VBA 就不会关心您是否移动单元格/按钮
  4. 如果您合并了为其创建命名范围的单元格,请记住电子表格中的命名范围仅指向左上角的单元格
    • 但是,合并区域的 Target.Address 返回整个范围,而不仅仅是一个单元格。如果您的Select Case 引用Target 左上角单元格的地址,则可以避免此问题。
    • 使用Target.Cells(1,1).Address
  5. 合并单元格的错误选择:不要使用 MergeArea.AddressMergeArea 不起作用 ];它返回单元格所在的合并范围。

在合并单元格上[

'How to Make Cells into Buttons that execute code
' place code in the specific Worksheet module of interest
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   ' in this example, I create named ranges on the spreadsheet '
            ' [complicated names, here, so you can read what's what]:
      ' one cell:  "Button_OneCellNameRange"
      ' one set of merged cells:  "Button_MergedCellNameRange"
         ' [the reference is the top-left cell, only]

   ' a VBA created cell/button location [not very useful for later sheet edits]
      Dim myVBACellButton As Range
      Set myVBACellButton = Range("B2")

         Debug.Print "Target Address: " & Target.Address
               'merged cells will return a range: eg "$A$1:$D$3"

         Debug.Print "Target.Cells(1,1).Address: " & Target.Cells(1, 1).Address
               'merged cells will return the top left cell, which would match
                  ' a named reference to a merged cell
   Select Case Target.Cells(1, 1).Address
        'if you have merged cells, you must use the ".cells(1,1).address"
        ' and not just Target.Address

      Case Is = "$A$1"
         MsgBox "Hello from: Click on A1"

      Case Is = myVBACellButton.Address
         MsgBox "Hello from: Click on B2, a VBA referenced cell/button"
            ' "myCellButton" defined as range in VBA

      'using a range named on the spreadsheet itself ...
         ' named ranges allow one to move the cell/button freely,
         ' without VBA worries
      Case Range("Button_OneCellNameRange").Address
         MsgBox "Hello From: Button Click on Button_OneCellNameRange"

      Case Range("Button_MergedCellNamedRange").Address
         'note that the address for merged cells is ONE CELL, the top left
         MsgBox _
            "Hello from: Button_MergedCellNamedRange.Address: " _
                  & Range("Button_MergedCellNamedRange").Address _

      Case Else ' normally you wouldn't be using this, for buttons
         MsgBox "NOT BUTTONS"

   End Select
End Sub

仅适用于单个单元格 .jpg" rel="nofollow noreferrer">工作表图像

An Alternative to "putting a button in a cell"

Make a cell selection execute code, directly, using the worksheet event: Worksheet_SelectionChange. Place the code in the specific sheet's module. Color/Border/Text the cell as you please. A cell-is-a-cell on any computer or screen. I use this to reference help loaded into a userForm, from a look up on a help sheet, when the user clicks on a short label/description. Using cell/buttons avoids Active-X objects complaints from IT.

Things to think on, with the sample code, following:

  1. Target.Address returns absolute addresses, using the "$" character
  2. Use the Select Case in your code, even if you have one cell/button. This eases the path to adding cell/buttons, later
  3. consider using named ranges on the spreadsheet, and reference them in the code. That way, VBA won't care if you move the cell/button
  4. If you have merged cells for which you create a named range, remember that the named range, in the spreadsheet, only points to the top-left cell
    • However, the Target.Address for the merged area returns the full range, not just one cell. If your Select Case refers to the address of the Target's top-left cell, you avoid this problem.
    • use Target.Cells(1,1).Address
  5. Bad choice for merged cells: don't use MergeArea.Address (MergeArea will not work on merged cells [only works on single cells]; it returns the merged range within which a cell lives.

*Sample Code*

'How to Make Cells into Buttons that execute code
' place code in the specific Worksheet module of interest
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   ' in this example, I create named ranges on the spreadsheet '
            ' [complicated names, here, so you can read what's what]:
      ' one cell:  "Button_OneCellNameRange"
      ' one set of merged cells:  "Button_MergedCellNameRange"
         ' [the reference is the top-left cell, only]

   ' a VBA created cell/button location [not very useful for later sheet edits]
      Dim myVBACellButton As Range
      Set myVBACellButton = Range("B2")

         Debug.Print "Target Address: " & Target.Address
               'merged cells will return a range: eg "$A$1:$D$3"

         Debug.Print "Target.Cells(1,1).Address: " & Target.Cells(1, 1).Address
               'merged cells will return the top left cell, which would match
                  ' a named reference to a merged cell
   Select Case Target.Cells(1, 1).Address
        'if you have merged cells, you must use the ".cells(1,1).address"
        ' and not just Target.Address

      Case Is = "$A$1"
         MsgBox "Hello from: Click on A1"

      Case Is = myVBACellButton.Address
         MsgBox "Hello from: Click on B2, a VBA referenced cell/button"
            ' "myCellButton" defined as range in VBA

      'using a range named on the spreadsheet itself ...
         ' named ranges allow one to move the cell/button freely,
         ' without VBA worries
      Case Range("Button_OneCellNameRange").Address
         MsgBox "Hello From: Button Click on Button_OneCellNameRange"

      Case Range("Button_MergedCellNamedRange").Address
         'note that the address for merged cells is ONE CELL, the top left
         MsgBox _
            "Hello from: Button_MergedCellNamedRange.Address: " _
                  & Range("Button_MergedCellNamedRange").Address _

      Case Else ' normally you wouldn't be using this, for buttons
         MsgBox "NOT BUTTONS"

   End Select
End Sub

Image of the Sheet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文