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

时间:2011-11-28 23:29:38

标签: excel vba button excel-vba activex

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

你能建议我这样做吗?

P.S它是一个activeX按钮

由于

2 个答案:

答案 0 :(得分:22)

您不能将任何对象“放入”单元格中,只能放在单元格上。您可以将按钮的Left和Top属性设置为Cell的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

答案 1 :(得分:0)

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

使用工作表事件Worksheet_SelectionChange直接使单元格选择执行代码。将代码放在特定工作表的模块中。根据需要为单元格上色/边框/文本。任何计算机或屏幕上的单元都是单元。当用户单击简短的标签/说明时,我使用它来引用从帮助表中查找到加载到userForm中的帮助。使用单元格/按钮可以避免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不适用于合并单元格[仅适用于单个单元格];它将返回单元格所处的合并范围。 / li>

*示例代码*

'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