我有一个消息框来插入用户选择的单元格范围。 我希望该单元格成为新的事件单元格。 执行此操作的 VBA 代码是什么?
这是我拥有的 VBA,但它不会更改事件单元格。
Sub AddClient()
'Message Box
Dim userResponce As Range
On Error Resume Next
Set userResponce = Application.InputBox("Place curser on Client Name that you want to insert new one above")
On Error GoTo 0
If userResponce Is Nothing Then
MsgBox "Cancel clicked"
Else
MsgBox "You selected " & userResponce.Address
End If
End Sub
最佳答案
如果您想让单元格处于事件状态,则可以使用Application.Goto
。如果您只想插入行,则不需要像 @BigBen 提到的那样使单元格处于事件状态。您可以直接插入行,而无需激活单元格或行。
例如
userResponce.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
还可以使用 Application.InputBox
和 Type:=8
,如 Application.InputBox method 所示
如果您想激活单元格,请使用以下命令。
Sub AddClient()
Dim userResponce As Range
Dim prmt As String
prmt = "Place cursor on Client Name that you want to insert new one above"
On Error Resume Next
Set userResponce = Application.InputBox(prompt:=prmt, Type:=8)
On Error GoTo 0
If userResponce Is Nothing Then
MsgBox "Cancel clicked"
Else
Application.Goto Reference:=userResponce
End If
End Sub
关于excel - 如何使用输入框引用单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58169238/