在 Excel VBA 中为用户提供范围选择工具/实用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4507350/
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
Provide a range selection tool/utility to the user in Excel VBA
提问by Vinay Pandey
I am trying to develop a user form in Excel 2007 VBA and want to provide a user with a range selection icon, something like this:
我正在尝试在 Excel 2007 VBA 中开发一个用户表单,并希望为用户提供一个范围选择图标,如下所示:
However, I have not been able to find any built-in form design tool or any online tool which provides this or at least gives me an idea. If anyone has any idea about this, I will greatly appreciate their help.
但是,我找不到任何内置的表单设计工具或任何提供此功能或至少给我一个想法的在线工具。如果有人对此有任何想法,我将非常感谢他们的帮助。
采纳答案by Codo
This control is called RefEditcontrol.
此控件称为RefEdit控件。
To use it, you have to first add it to the toolbox window by right-clicking in the toolbox window and selecting Additional Controls.... Then you select RefEdit.Ctrland close the dialog.
要使用它,您必须首先通过在工具箱窗口中右键单击并选择Additional Controls...将其添加到工具箱窗口中。然后选择RefEdit.Ctrl并关闭对话框。
Now you can select it in the toolbox and place it on your form.
现在您可以在工具箱中选择它并将其放置在您的表单上。
回答by k rey
Another alternative to using the RefEdit.Ctrlis to hook into some undocumented features of the TextBoxcontrol and use the Application.InputBoxfunction.
使用RefEdit.Ctrl 的另一种替代方法是挂钩TextBox控件的一些未记录的功能并使用Application.InputBox函数。
There are two properties of the TextBox control that do not appear in the Properties dialog, that allow you to add a button on the right. They are DropButtonStyleand ShowDropButtonWhen. When the button is clicked it will fire the DropButtonClickevent for the control where you can show the input box.
TextBox 控件的两个属性没有出现在“属性”对话框中,它们允许您在右侧添加按钮。它们是DropButtonStyle和ShowDropButtonWhen。单击按钮时,它将触发控件的DropButtonClick事件,您可以在其中显示输入框。
Start by placing a TextBoxcontrol on the form. Then add the following to the UserForm_Initializeprocedure:
首先在窗体上放置一个TextBox控件。然后将以下内容添加到UserForm_Initialize过程中:
Private Sub UserForm_Initialize()
txtRefersTo.DropButtonStyle = frmDropButtonStyleReduce
txtRefersTo.ShowDropButtonWhen = frmShowDropButtonWhenAlways
End Sub
Then add an event handler to the DropButtonClickevent as follows to capture the range using the Application.InputBoxdialog:
然后向DropButtonClick事件添加一个事件处理程序,如下所示,使用Application.InputBox对话框捕获范围:
Private Sub txtRefersTo_DropButtonClick()
Me.Hide
txtRefersTo.Text = Application.InputBox("Select the range", "Range Picker", txtRefersTo.Text, Type:=8)
Me.Show vbModal
End Sub
The main advantage to this approach is that it allows you to place a control within a frame or on a separate tab without experiencing the issues associated with the RefEdit.Ctrl. The disadvantage is that it requires a separate dialog to interact with Excel.
这种方法的主要优点是它允许您将控件放置在框架内或单独的选项卡上,而不会遇到与RefEdit.Ctrl相关的问题。缺点是它需要一个单独的对话框来与 Excel 交互。