vba 有没有办法编辑表单控件按钮上的标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21578281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to edit caption on Form Control buttons?
提问by Josh
Is it possible (on a sheet change event), to change the caption text of a Form Control button?
是否可以(在工作表更改事件中)更改表单控件按钮的标题文本?
Ideally, I would like to add a variable into the existing button caption depending on other data in the workbook.
理想情况下,我想根据工作簿中的其他数据将变量添加到现有按钮标题中。
回答by
Well, right click the Sheet1 tab on the bottom of spreadsheet and select view code
好吧,右键单击电子表格底部的 Sheet1 选项卡并选择查看代码
Copy paste the following code
复制粘贴以下代码
Private Sub Worksheet_Activate()
Buttons("Button 1").Caption = "NEW TEXT"
End Sub
This event fires up when Sheet1 gets activated and it changes the caption on the button to NEW TEXT
当 Sheet1 被激活并将按钮上的标题更改为 NEW TEXT
Obviously you can refer to your buttons either by name or their index. You would to modify the "Button 1"
if you want to modify the text on a different object.
显然,您可以通过名称或索引来引用您的按钮。你会修改"Button 1"
,如果要修改一个不同的对象上的文字。
You can assign it a variable or evaluation of an expression if you replace the "NEW TEXT"
如果您替换 "NEW TEXT"
For example
例如
if on Sheet2
you type anything in the cell A1 you can modify the code behind Sheet1 and use
如果在Sheet2
单元格 A1 中键入任何内容,则可以修改 Sheet1 后面的代码并使用
Private Sub Worksheet_Activate()
Buttons("Button 1").Caption = Sheets("Sheet2").Range("A1")
End Sub
this way every time you activate Sheet1 the text on a button will be picked up from Sheet1, Cell A1
这样每次激活 Sheet1 时,按钮上的文本都会从 Sheet1,Cell A1 中选取
To find out the name of the button look here
要找出按钮的名称,请看这里
回答by Aba
I needed TextFrame.Characters.Textto get to the caption
我需要TextFrame.Characters.Text才能看到标题
Worksheets("Sheet1").Shapes("button 1").TextFrame.Characters.Text = "Click"
回答by dee
In this example Caption of Form-Control Button with name 'Button 1' is changed according to value of cell changed in 'Sheet1'. The button is on Sheet1, the code comes to Class-module of Sheet1. HTH
在此示例中,名称为“Button 1”的 Form-Control Button 的标题根据“Sheet1”中更改的单元格值进行更改。按钮在Sheet1上,代码来到Sheet1的Class-module。HTH
' Sheet1 module:
Private Sub Worksheet_Change(ByVal Target As Range)
Const buttonName As String = "Button 1"
Dim targetButton As Excel.Button
On Error Resume Next
Set targetButton = Target.Worksheet.Buttons(buttonName)
On Error GoTo 0
If targetButton Is Nothing Then _
Exit Sub
If Target.Value <> "" Then _
targetButton.Caption = Target.Value
End Sub
回答by Jimit Rupani
Try this :
尝试这个 :
ActiveSheet.Shapes("Button 1").Select
Selection.Characters.Text = "My New Caption"