问题
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
回答1:
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
回答2:
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:
- Target.Address returns absolute addresses, using the "$" character
- Use the
Select Case
in your code, even if you have one cell/button. This eases the path to adding cell/buttons, later - 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
- 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 yourSelect Case
refers to the address of theTarget
's top-left cell, you avoid this problem. - use
Target.Cells(1,1).Address
- However, the
- 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
来源:https://stackoverflow.com/questions/8303887/place-a-command-button-in-a-cell-ms-excel-vba