Excel 如何使用输入框引用单元格?
我有一个消息框来插入用户选择的单元格范围。 我想让那个细胞成为新的活跃细胞。 VBA代码是如何做到这一点的 这是我的VBA,但它不会改变活动单元格Excel 如何使用输入框引用单元格?,excel,vba,Excel,Vba,我有一个消息框来插入用户选择的单元格范围。 我想让那个细胞成为新的活跃细胞。 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
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一起使用,如中所示
如果要激活该单元格,请使用下面的选项
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
userresponse.选择
另外,为指定类型
-类型:=8
.@ScottCraner的解决方案是答案-但您可能需要一个userresponse.Parent.在选择
之前激活
,以防工作表尚未激活。尽管通常您不需要选择
。您可以插入
一行而无需选择
ing。谢谢您-但我想我不知道在哪里插入您的解决方案。我尝试了各种方法,但每次都有错误。Siddharth下面的详细解决方案非常有效。