如何让用户从对话框中输入范围

时间:2017-09-28 14:49:29

标签: excel vba excel-vba

所以我需要从用户那里获得一个范围,如何查询用户选择范围,例如"

dim x as range
x = getrange("Select Range to Compare")
msgbox "The range selected is " & x

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情。根据您的要求调整它。

Sub AskUserToSelectARangeToWorkWith()
Dim Rng As Range
On Error Resume Next
Set Rng = Application.InputBox("Select a Range to compare.", "Select A Range!", Type:=8)
If Rng Is Nothing Then
    MsgBox "You didn't select a Range.", vbCritical, "No Range Selected!"
    Exit Sub
End If
MsgBox "The Range selected is " & Rng.Address(0, 0)
End Sub