我有以下代码在特定单元格的工作表上放置按钮。
Sub insertRowButtons()
Dim btn As Button
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete
Dim t As Range
'to show details for all groups
ActiveSheet.Outline.ShowLevels rowlevels:=5
'use this lines for rows
Dim arr As Variant
arr = Array(20, 32, 44, 56)
For i = 0 To UBound(arr)
Set t = ActiveSheet.Range(Cells(arr(i), 4), Cells(arr(i), 4))
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
.OnAction = "insertItemRows"
.Caption = "Btn " & i
.Name = "Btn" & i
End With
Next i
'set group detail back to standard view
ActiveSheet.Outline.ShowLevels rowlevels:=4
Application.ScreenUpdating = True
End Sub
当我运行代码时,一切正常,只是放置的按钮没有将“锁定纵横比” - “选项”设置为“真”。如您所见,请参见下图,非常需要锁定宽高比。当我插入按钮时,下面隐藏了一些行。
当行被隐藏并且我放置按钮并随后取消隐藏行时,按钮会自行调整大小。
(带红叉的按钮是手动锁定宽高比,蓝色十字没有锁定)
请告诉我如何设置"锁定纵横比"在代码中为true而不通过按钮"格式控制"。
手动执行一如既往,提前谢谢。
答案 0 :(得分:2)
看起来你只能在形状对象上使用LockAspectRatio,所以在循环中尝试这个:
With btn
.OnAction = "insertItemRows"
.Caption = "Btn " & i
.Name = "Btn" & i
ActiveSheet.Shapes(.Name).LockAspectRatio = msoTrue
End With