使用 VBA 提示用户选择单元格(可能在不同的工作表上)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22812235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using VBA to prompt user to select cells (possibly on different sheet)
提问by Jonny
I'm working in Excel on a VBA project, and want part of my macro to prompt the user to select a range of cells*, which the macro can later do stuff with.
我正在 Excel 中处理 VBA 项目,并希望我的宏的一部分提示用户选择一系列单元格*,宏稍后可以使用这些单元格。
*The type of prompt you get when creating a chart, or using a GUI to insert a function
*创建图表或使用 GUI 插入函数时得到的提示类型
e.g. here:
例如这里:
and here:
和这里:
I'm therefor looking for something along the lines of
因此,我正在寻找类似的东西
Sub MyMacro()
MsgBox "Please select data range"
' allow user to select range (as images above)
CreateFunctionArgumentsPrompt()
'do stuff with user selected range of cells
...
End Sub
Is it possible to access built-in Excel functionality to perform what I refer to as: CreateFunctionArgumentsPrompt()
是否可以访问内置的 Excel 功能来执行我所说的: CreateFunctionArgumentsPrompt()
Note: this is similar to SO question excel vba get range of user selected range by mousebut differs in that
注意:这类似于 SO question excel vba get range of user selected range by mouse但不同之处在于
- I want to use the built in GUI functionality of Excel as displayed above
- I need to be able to select and refer to a range on a sheet other than the active worksheet
- 我想使用上面显示的 Excel 的内置 GUI 功能
- 我需要能够选择和引用活动工作表以外的工作表上的范围
回答by ChrisProsser
This isn't using the built in that you showed above, but does allow you to select a range of cells following an income prompt:
这不是使用您上面显示的内置函数,而是允许您根据收入提示选择一系列单元格:
Sub RangeSelectionPrompt()
Dim rng As Range
Set rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
MsgBox "The cells selected were " & rng.Address
End Sub
This is based on the answer given in this MrExcel answer.
这是基于此 MrExcel answer 中给出的答案。
Here is how it looks in use:
这是它在使用中的样子:
回答by DanK
What you are looking for is a dialog box (also called a common dialog). Unfortunately you cannot add one to the existing built in objects(at least not using VBA).
您正在寻找的是一个对话框(也称为公共对话框)。不幸的是,您不能向现有的内置对象添加一个(至少不使用 VBA)。
As mentioned above you can mimic this functionality using InputBox and Forms. That said I have seen proprietary programs that are based on Excel where the company added the type of functionality you describe. However, I believe you have to use C++ or a deeper language to create DLLs that can accomplish this
如上所述,您可以使用 InputBox 和 Forms 来模拟此功能。也就是说,我已经看到基于 Excel 的专有程序,该公司添加了您描述的功能类型。但是,我相信您必须使用 C++ 或更深层次的语言来创建可以实现此目的的 DLL
One thing worth noting about dialogs: Excel has a built in Common File Dialog object librarywhich allows you to create common file server dialog boxes (such as Open, Save & Select) using existing Windows API dialogs.
关于对话框的一件事值得注意:Excel 有一个内置的通用文件对话框对象库,它允许您使用现有的 Windows API 对话框创建通用文件服务器对话框(例如打开、保存和选择)。