Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 707
Ank Avatar asked Nov 28 '11 23:11

Ank


1 Answers

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
like image 137
Tim Williams Avatar answered Sep 30 '22 07:09

Tim Williams