EXCEL VBA 关于函数和用户表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13945672/
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
EXCEL VBA regarding functions & userforms
提问by 4 Leave Cover
I am new in Excel VBA programming therefore I've met a few difficulties within my very own project.
我是 Excel VBA 编程新手,因此在我自己的项目中遇到了一些困难。
Long story short, I have 10 UserForm(s), and 10 TextBox(s) in each UserForm with the same TextBox name.
长话短说,我在每个用户窗体中有 10 个用户窗体和 10 个具有相同文本框名称的文本框。
Is it possible for me to create a function that I can call it in every UserForm so that I don't have to manually code it for every UserForm(s). Regarding the function, it will reset the value of TextBox, as well as bring it back to the 'initial' state.
我是否可以创建一个可以在每个用户窗体中调用它的函数,这样我就不必为每个用户窗体手动编码它。关于函数,它将重置 TextBox 的值,并将其带回“初始”状态。
That's all for my question. Any help would be highly appreciate.Thanks in advance.
这就是我的问题。任何帮助将不胜感激。提前致谢。
采纳答案by chris neilsen
Create a Sub
in a Module
that you call from each form
Sub
在Module
您从每个表单调用的a中创建一个
eg
例如
Sub TextBoxInit(tb As MSForms.TextBox)
tb.Text = ""
End Sub
Call it in each UserForm
as you see fit
在每个UserForm
你认为合适的地方调用它
eg
例如
Private Sub Userform_Initialize()
Module1.TextBoxInit TextBox1
End Sub
回答by Elias
Tan,
谭,
You should think about making use of Class Modules. I actually just learned about them recently, and they are a very effective way to manage multiple userforms with similarly constructed controls.
您应该考虑使用Class Modules。实际上,我最近才了解它们,它们是一种非常有效的方法,可以使用类似构造的控件来管理多个用户表单。
Go Here: VBA USERFORM: PREVENT COMBOBOX ESCAPE ON KEYDOWN
转到此处:VBA 用户表单:在 KEYDOWN 上防止组合框转义
Or Here: EXCEL VBA: dblClick, Repetitive Code Improvement
或在这里:EXCEL VBA:dblClick,重复代码改进
The basics of how these Class Modules work is that you create your own object, you write the code for it, and you assign your desired userform controls as that object type (class). For instance, if you had 20 textboxes which performed the same way (they rebooted their own values), you would just write that identical code in the class module section. Bam! That's pretty much it (you'd also have to write a paragraph of code to loop through & assign which textboxes you'd want this functionality for).
这些类模块如何工作的基础是您创建自己的对象,为其编写代码,并将所需的用户窗体控件分配为该对象类型(类)。例如,如果您有 20 个以相同方式执行的文本框(它们重新启动了自己的值),您只需在类模块部分编写相同的代码。砰!差不多就是这样(您还必须编写一段代码来循环并分配您想要此功能的文本框)。
It might take a while to understand, but it will significantly improve your project.
可能需要一段时间才能理解,但它会显着改善您的项目。