excel - 如何使用输入框引用单元格?

标签 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 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.InputBoxType:=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/

相关文章:

vba - 使用 xlsm 文件等模板创建文件夹和新的 *.xlsx 文件

vb.net - 通过 COM 接口(interface)向 VBA 公开 .NET DataTable 属性

vba - 单击时出现错误 "Argument is not optional"

excel - 基于 excel/google 表中的列和行的平均值

excel - 有没有一种简单的方法来检查字符串是否以字母开头(任何 4 个字母)?

VBA 无法发送日历约会

excel - 如何在VBA中更改图表颜色

java - 如何用java比较2个excel中的数据

excel - 在 Perl 中读取文件、匹配字符串和格式化输出

vba - Excel输入框最多打印多少份