我是VBA的新手。我已经录制了宏,但我自己没有编写任何代码。我试图从用户那里获得输入并将其放入单元格中。
我的尝试代码如下。
Sub test ()
Dim myvar as string
myvar = "testname"
'Put item in cell
End sub
答案 0 :(得分:2)
下面提供的代码将从最终用户获得输入,在此示例中将其放入单元格A1,并通知您它已成功。
Sub Test() 'Declare Sub
Dim InputValue As String 'Declare the string
InputValue = InputBox("Enter text...") 'Get input from end user
Range("A1").Value = InputValue 'Place input into cell A1
MsgBox "Your input """ & InputValue & """ has been placed in cell A1" 'Message box indicate it was placed in A1
End Sub
答案 1 :(得分:1)
使用inputbox
Sub test ()
Dim myVar as string
myVar = InputBox("Please, enter your name:","Name Info Required","testname")
'Put item in cell
ThisWorkBook.Sheets(1).Cells(1,1).Value = myVar
End sub
答案 2 :(得分:0)
除了你在这里得到的答案之外,请查看使用Application.InputBox
,这样可以限制用户提供的输入。
Sub Test()
Dim myVar As String
myVar = Application.InputBox(Prompt:="Enter text (as String)...", Type:=2)
' the rest is the same as the answers above
Range("A1").Value = myVar
End Sub
要详细了解Application.InputBox
的选项,请参阅MSDN