vba 如何将用户表单作为图标/按钮添加到功能区并显示用户表单但仍然能够使用 Excel 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10489204/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:04:09  来源:igfitidea点击:

How to add user form as an icon/button to ribbon and show userform but still able to work with Excel File?

excel-vbavbaexcel

提问by NCC

When I user the user form, I have to:

当我使用用户表单时,我必须:

1) Alt + F11

1) Alt + F11

2) Choose the Form

2) 选择表格

3) Run

3)运行

4) Close the Form

4) 关闭表格

5) Go back to Excel

5)回到Excel

Excel will not allow me to do anything if the form is not closed. Is there anyway to let me put a little icon on ribbon? And keep the user form appear while I am working with Excel?

如果表单未关闭,Excel 将不允许我做任何事情。无论如何让我在功能区上放一个小图标?并在我使用 Excel 时保持用户表单出现?

采纳答案by Geoff

You've got two parts to your question:

你的问题有两个部分:

Adding an icon to the ribbon: Do you want the macro to be available for all spreadsheets?

向功能区添加图标:您希望宏可用于所有电子表格吗?

  • If so, follow this guideto save your macro as an Excel Add-in, and then attach it to the ribbon.
  • If you only need it in the current spreadsheet, you could simplify things by adding a button to the spreadsheet which activates the macro (use this guide), or you could use a shortcut key to invoke the macro directly (use this guide, Assigning a shortcut to an existing macrosection)
  • 如果是这样,请按照本指南将宏另存为 Excel 加载项,然后将其附加到功能区。
  • 如果您只在当前电子表格中需要它,您可以通过在电子表格中添加一个按钮来激活宏来简化事情(使用本指南),或者您可以使用快捷键直接调用宏(使用本指南Assigning a shortcut to an existing macro部分)

Keeping the dialog open:One of the properties of the UserForm is ShowModal; you can simply set that to false. Alternatively, as per the other answer, you can open it with MyForm.Show vbModeless.

保持对话框打开:用户窗体的属性之一是ShowModal;您可以简单地将其设置为 false。或者,根据另一个答案,您可以使用MyForm.Show vbModeless.

Note that the properties of the form also allow you to provide a specific screen position too, so that the form isn't in the way while you're working: change StartUpPositionto 0 - Manual, and provide a value for Topand Left.

请注意,窗体的属性还允许您提供特定的屏幕位置也让形式是不是在路上,而你的工作:改变StartUpPosition0 - Manual,并提供一个值TopLeft

回答by Siddharth Rout

You don't need to do that :) You can simply launch the form in modelessmode to keep it open and work with the Excel file at the same time

您不需要这样做 :) 您可以简单地以modeless模式启动表单以使其保持打开状态并同时处理 Excel 文件

Try this to launch the userform.

试试这个来启动用户表单。

Sub Sample()
    Userform1.Show vbModeless
End Sub

回答by mon

I think you should have to create another module and call the userform in that module. After that just put that macro on the ribbon.It may help you.....

我认为您应该创建另一个模块并在该模块中调用用户表单。之后只需将该宏放在功能区上即可。它可能会帮助您.....