文本输入框中的参数

时间:2013-10-02 00:49:15

标签: excel vba

弹出文本框中输入“输入名称”需要哪些VBA代码,以便将此名称粘贴到三个不同的位置:Sheets("Data").Select A15A50 (两个我不想搞砸的超链接),第三个是Sheets("Run").Select,这是一个按钮。

所有这三个地方都被称为Other 1,我希望能够使用弹出窗口中的输入信息更改它们。

Sub TestMacro() 
    Dim Name As String 
    Name = InputBox("Enter Name.") 
    Range("A" & 15).Value = Name 
    Range("A" & 50).Value = Name 
    Sheets("Run").Select 
    Range("A" & 1).Value = Name 
    Sheets("Data").Select 
End Sub

1 个答案:

答案 0 :(得分:1)

我想我已经找到了适合你的东西。

Sub TestMacro() 

    Dim Name As String 

    Name = InputBox("Enter Name.") 
    If StrPtr(Name) = 0& Then Exit Sub 'user cancelled out of inputbox!

    SetCellValue Sheets("Data"), "A15", Name
    SetCellValue Sheets("Data"), "A50", Name 
    SetCellValue Sheets("Run"), "A1", Name

    'Forms button can be accessed with the "Buttons" collection of "Worksheet" object:
    Sheets("Run").Buttons(1).Caption = Name 'assumes it's the first button in the collection

    'ActiveX button can be accessed directly by its programmatic name:
    Sheet1.CommandButton1.Caption = Name 'assumes sheet "Run" is Sheet1

End Sub

Private Sub SetCellValue(xlSheet As Worksheet, addr As String, value As Variant)
    xlSheet.Range(addr).Value = value
End Sub