通过VBA插入表单控制按钮 [英] Insert form control button via VBA
问题描述
我想通过VBA在我的工作表中插入一个 button
.因此,我尝试使用
I want to insert a button
via VBA into my worksheet. Therefore, I tried to go with the simple VBA from here.
Sub Insert_Button()
Sheet1.Shapes("Button 1").Visible = True
End Sub
但是,只有在手动插入和隐藏按钮之前,此VBA才起作用.
我还在此处中尝试了该解决方案,但将代码标记为红色和没有运行.
However, this VBA only works if the button has been inserted and hidden manually before.
I also tried the solution from here but it marked the code red and did not run.
如果文件中不存在表单控制按钮
,该如何通过VBA插入?
How can I insert a form control button
via VBA if the button does no exist in the file yet?
推荐答案
这应该有效:
ActiveSheet.Buttons.Add(185.25, 61.5, 85.5, 24.75).Select
数字是坐标,因此,如果要将按钮添加到单元格,则必须使用单元格的左/上......
The numbers are co-ordinates, so if you want to add the button to a cell, you would have to use the cell's left/top... like this
With ActiveSheet
.Buttons.Add(.Cells(2, 2).Left, .Cells(2, 2).Top, 85.5, 24.75).Select
End With
包括"A1:B2"之类的范围的宽度和高度...
To include the width and height of a Range like "A1:B2"...
With ActiveSheet
.Buttons.Add(.Cells(1, 1).Left, .Cells(2, 2).Top, .Cells(1, 1).Width + .Cells(1, 2).Width, .Cells(1, 1).Height + .Cells(2, 2).Height).Select
End With
这篇关于通过VBA插入表单控制按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!