Search code examples
vbabuttonexcelactivex

Place a command button in a cell MS Excel vba


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


Solution

  • 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