阻止用户选择多个单元格

时间:2022-05-13 22:23:15

I am using the following code to let users select the cell they want to edit.

我使用以下代码让用户选择他们想要编辑的单元格。

Application.InputBox(Prompt:="Click the cell you want to edit.", Title:="Cell To Edit", Type:=8)

How can I change my code so they can only select one cell at a time?

如何更改我的代码,以便他们一次只能选择一个单元格?

2 个解决方案

#1


1  

Is this what you are trying?

这是你在尝试什么?

Sub Sample()
    Dim r As Range

    Do
        On Error Resume Next
        Set r = Application.InputBox(Prompt:="Click the single cell you want to edit.", _
                                     Title:="Cell To Edit", _
                                     Type:=8)
        On Error GoTo 0

        If r Is Nothing Then Exit Sub

        If r.Cells.Count = 1 Then
            Exit Do
        Else
            MsgBox "Please select a single cell only"
            Set r = Nothing
        End If
    Loop

    'MsgBox r.Address
End Sub

#2


0  

How about:

怎么样:

Sub qwerty()
    Dim r As Range
    Set r = Range("A1:A2")
    While r.Count <> 1
        Set r = Application.InputBox(Prompt:="Click the cell you want to edit.", Title:="Cell To Edit", Type:=8)
    Wend
End Sub

#1


1  

Is this what you are trying?

这是你在尝试什么?

Sub Sample()
    Dim r As Range

    Do
        On Error Resume Next
        Set r = Application.InputBox(Prompt:="Click the single cell you want to edit.", _
                                     Title:="Cell To Edit", _
                                     Type:=8)
        On Error GoTo 0

        If r Is Nothing Then Exit Sub

        If r.Cells.Count = 1 Then
            Exit Do
        Else
            MsgBox "Please select a single cell only"
            Set r = Nothing
        End If
    Loop

    'MsgBox r.Address
End Sub

#2


0  

How about:

怎么样:

Sub qwerty()
    Dim r As Range
    Set r = Range("A1:A2")
    While r.Count <> 1
        Set r = Application.InputBox(Prompt:="Click the cell you want to edit.", Title:="Cell To Edit", Type:=8)
    Wend
End Sub