自動適應儲存格大小的按鈕

添加按鈕至指定儲存格,完美貼合並自動適應大小

Sub CreateFormsButton()
 Dim btn As Button
 Dim rng As Range
 With Worksheets("Sheet1")
 Set rng = .Range("C5")
 Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
 With btn
 .Caption = "Test"
 .OnAction = "Test1"
 End With
 End With
 End Sub

添加activex按鈕至指定儲存格,完美貼合並自動適應大小

Sub CreateCommandButton()
 Dim ctop#, cleft#, cht#, cwdth#
 Dim sht As Worksheet
 Dim Btn As OLEObject
 Set sht = ThisWorkbook.Worksheets("Sheet1")
 With Range("C10")
 ctop = .Top
 cleft = .Left
 cht = .Height
 cwdth = .Width
 End With
 With sht
 Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=cleft, Top:=ctop, Width:=cwdth, Height:=cht)
 End With
 Btn.Object.Caption = "Click Me"
 Btn.Name = "MyButton"
 Btn.Placement = xlMoveAndSize
 'Optional code insertion - - establish ref in VBE to MS VBA Extensibility 5.3 Library
 With ThisWorkbook.VBProject.VBComponents(sht.CodeName).CodeModule
 .InsertLines .CreateEventProc("Click", Btn.Name) + 1, "Msgbox ""Replace this message with your actual code."" "
 End With
 End Sub

1人評論了“自動適應儲存格大小的按鈕”

發表評論

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

五 × 三 =

購物車