Sub Add_Button(my_top, my_left, my_Width, my_Height)
Dim myBtn As Object, i As Integer
Set myBtn = ActiveSheet.Buttons.Add(1, 1, 1, 1)
With myBtn
.Top = Cells(my_top, my_left).Top
.Left = Cells(my_top, my_left).Left
.Height = Cells(my_top, my_left).Height * my_Height
.Width = Cells(my_top, my_left).Width * my_Width
.Caption = text
End With
End Sub
所以我认为代码非常简单。我用以下代码调用这个 Sub
Sub Test_Add_Button()
my_top = 2
my_left = 60
my_Width = 2
my_Height = 3
Add_Button my_top, my_left, my_Width, my_Height
End Sub
Sub ADD_A_BUTTON()
Dim btn As Button
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete
Dim t As Range
For i = 2 To 6 Step 2 'IF YOU ADD MULTIPLE BUTTONS IF ONLY ONE THEN MAKE THE ROW NUMBER YOU WANT TO THE EXACT SAME ROW NUMBER EG. FOR I 2 TO 2
Set t = ActiveSheet.Range(Cells(i, 3), Cells(i, 3)) 'THE COLUMN TO PLACE
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
.OnAction = "btnS"
.Caption = "Btn " & i
.Name = "Btn" & i
End With
Next i
Application.ScreenUpdating = True
End Sub
Public Sub Add_Button(Target As Range, Caption As String)
Dim myBtn As Object
Set myBtn = Target.Parent.Buttons.Add(Target.Left, Target.Top, Target.Width, Target.Height)
myBtn.Caption = Caption
End Sub
Public Sub Test()
Add_Button ThisWorkbook.Worksheets("Sheet1").Range("H2:H5"), "My Button"
End Sub