【知识点】 显示用于用户输入的对话框。 返回在对话框中输入的信息。 语法 expression. InputBox (_Prompt_, _Title_, _Default_, _Left_, _Top_, _HelpFile_, _HelpContextID_, _Type_) expression:一个表示 Application 对象的变量。 注解 下表列出了可以在 Type 参数中传递的值。 可以是一个值,也可以将多个值相加。 例如,对于可接受文本和数字的输入框,将 Type设置为 1 + 2。 -------------------- InputBox 可用于显示简单对话框,方便用户输入要在宏中使用的信息。 对话框中有“确认”**** 按钮和“取消”**** 按钮。 如果选择“确认”**** 按钮,InputBox 返回在对话框中输入的值。 如果选择“取消”**** 按钮,InputBox 返回 False。 如果 Type 为 0,InputBox 返回文本格式的公式(例如,=2*PI()/360)。 只要公式中有引用,就会以 A1 样式引用的形式返回。 (ConvertFormula 可用于转换引用样式。) 如果 Type 为 8,InputBox 返回 Range 对象。 必须使用 Set 语句,将结果分配给 Range 对象,如下面的示例所示。 Set myRange = Application.InputBox(prompt := 'Sample', type := 8) 如果不使用 Set 语句,此变量就会被设置为区域中的值,而不是 Range 对象本身。 如果使用 InputBox 方法提示用户输入公式,必须使用 FormulaLocal 属性,将公式分配给 Range 对象。 输入公式使用用户语言。 InputBox 方法与 InputBox 函数的区别在于,前者可以对用户输入进行选择性验证,并能与 Excel 对象、错误值和公式结合使用。 请注意,Application.InputBox 调用的是 InputBox 方法;不带对象限定符的 InputBox 调用的是 InputBox 函数。 ------------------------ =====官方文档中的例子======: 此示例使用 InputBox,允许用户选择要传递给用户定义的函数“MyFunction”的区域,这将乘以区域中的三个值并返回结果。 Sub Cbm_Value_Select() 'Set up the variables. Dim rng As Range 'Use the InputBox dialog to set the range for MyFunction, with some simple error handling. Set rng = Application.InputBox('Range:', Type:=8) If rng.Cells.Count <> 3 Then MsgBox 'Length, width and height are needed -' & _ vbLf & 'please select three cells!' Exit Sub End If 'Call MyFunction by value using the active cell. ActiveCell.Value = MyFunction(rng) End Sub Function MyFunction(rng As Range) As Double MyFunction = rng(1) * rng(2) * rng(3) End Function ===========学习例子========= 解决问题: 1.inputbox的“取消”按钮的处理 2.取得Application.inputbox选择区域的起止行列号和终止行列号 简单说:求“左上”“右上”“左下”“右下”的4的数据 =================== '====取得列号 Sub test() Dim myRange As Range Set myRange = Application.InputBox(prompt:='请选择区域:', Type:=8) r = myRange.Column MsgBox r End Sub ===================== '==取得Application.inputbox选择区域的起止行列号和终止行列号====代码2 Sub getinput_col_row() Dim myRange As Range, c As Range On Error Resume Next Set myRange = Application.InputBox(prompt:='请在工作表中选择区域:', Title:='请选择', Type:=8) On Error GoTo 0 If myRange Is Nothing Then MsgBox '你按了“取消”' Exit Sub End If arr = Array('起始行', '起始列', '终止行', '终止列') '取得选择区域的总的单元格数,其中.Address是取得 xx的引用 t = Range(myRange.Address).Count t2 = myRange.Cells.Count MsgBox '你总共选中的单元格数有:' & t2 brr = Array(Range(myRange.Address).Cells(1).Row, Range(myRange.Address).Cells(1).Column, Range(myRange.Address).Cells(t).Row, Range(myRange.Address).Cells(t).Column) 'MsgBox arr(0) & vbLf & brr(0) & vbLf & arr(1) & vbLf & brr(1) & vbLf & arr(2) & vbLf & brr(2) & vbLf & arr(3) & vbLf & brr(3) For i = 0 To UBound(arr) Text = Text & arr(i) & vbLf & brr(i) & vbLf Next MsgBox Text End Sub ===================== '===取得Application.inputbox选择区域的起止行列号和终止行列号==代码2 Sub test2() Dim myRange As Range, c As Range Set myRange = Application.InputBox(prompt:='请选择区域:', Type:=8) For Each c In myRange.Areas MsgBox Format(c(1).Row, '起始行号:0') & Format(c(1).Column, ' 起始列号:0') MsgBox Format(c(c.Count).Row, '终止行号:0') & Format(c(c.Count).Column, ' 终止列号:0') Next End Sub |
|
来自: 满泉ca85upjdlw > 《Excel知识》