VBA 窗体可以容纳的最大控件数是多少?

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

What is the maximum number of controls that a VBA form can hold?

formsvbaexcel-vbaexcel-2003limits

提问by Lunatik

I'm currently building an Excel 2003 app that requires a horribly complex form and am worried about limitations on the number of controls. It currently has 154 controls (counted using Me.Controls.Count- this should be accurate, right?) but is probably only about a third complete. The workflow really fits a single form, but I guess I can split it up if I really have to.

我目前正在构建一个 Excel 2003 应用程序,它需要一个非常复杂的表单,并且担心控件数量的限制。它目前有 154 个控件(使用计数Me.Controls.Count- 这应该是准确的,对吧?)但可能只有三分之一完成。工作流程确实适合单一形式,但我想如果我真的需要,我可以将其拆分。

I see evidence in a Google search that VB6 (this usually includes VBA) has a hard limit of 254 controls in a form. However, I created a dummy form with well over 1200 controls which still loaded and appeared to work just fine.

我在 Google 搜索中看到证据表明 VB6(通常包括 VBA)在表单中具有 254 个控件的硬限制。但是,我创建了一个包含超过 1200 个控件的虚拟表单,这些控件仍然可以加载并且看起来工作正常。

I did get some 'out of memory' errors when trying to add specific combinations of controls though, say 800 buttons and 150 labels, leading me to think that any limit might be affected by the memory requirements of each type of control.

在尝试添加特定的控件组合时,我确实遇到了一些“内存不足”错误,比如 800 个按钮和 150 个标签,这让我认为任何限制都可能受到每种控件的内存要求的影响。

Does anyone have any information that might help ensure that I or, more importantly, other users with differing environments don't run into any memory issues with such a large form?

有没有人有任何信息可以帮助确保我,或者更重要的是,具有不同环境的其他用户不会遇到如此大表单的任何内存问题?

采纳答案by Alex K.

Most MSForms controls are windowless (as in they are not true windows, rather they are drawn directly by the VBA Forms engine as graphical objects) which are "light-weight" by comparison. This means you can dump more onto a Userform than you could using equivalent non-MSForms controls on a VB6 form.

大多数 MSForms 控件都是无窗口的(因为它们不是真正的窗口,而是由 VBA Forms 引擎直接绘制为图形对象),相比之下它们是“轻量级”的。这意味着与在 VB6 表单上使用等效的非 MSForms 控件相比,您可以将更多内容转储到用户表单上。

I don't know what the upper limit is, but it will either be an absolute limit or a limit imposed by available resources, so if you can add 1,200 without encountering either of those & excel is behaving itself in terms of memory use you should be ok.

我不知道上限是什么,但它要么是绝对限制,要么是可用资源强加的限制,所以如果您可以添加 1,200 而不会遇到其中任何一个,并且 excel 在内存使用方面表现良好,您应该没事。

That said, that number of controls still seems an awful lot to present to the user at once!

也就是说,一次向用户呈现的控件数量似乎仍然很多!

回答by Stewbob

There is no hard limit for the maximum number of controls on a form. As mentioned in another answer, this will vary based on your hardware, Excel version, and operating system.

表单上的最大控件数没有硬性限制。正如另一个答案中提到的,这将根据您的硬件、Excel 版本和操作系统而有所不同。

Sadly, I have had too much experience at building VBA forms with too many controls. :(

遗憾的是,我在使用太多控件构建 VBA 表单方面有太多经验。:(

I can say, that once you get above about 200 controls, you may start noticing some strange/intermittent occurrences/errors.

我可以说,一旦超过大约 200 个控件,您可能会开始注意到一些奇怪的/间歇性的发生/错误。

One thing I have found, completely by trial and error, is that the Frame control, typically used to hold groups of radio buttons, seems to cause more problems than any other control. If I create forms without any Frame controls, I can get more controls on that form before running into trouble.

我完全通过反复试验发现的一件事是,通常用于保存单选按钮组的 Frame 控件似乎比任何其他控件引起的问题更多。如果我创建的表单没有任何 Frame 控件,我可以在遇到麻烦之前在该表单上获得更多控件。

I have found that no matter how many controls you need, they can usually by categorized into different groups. If a particular group or category is going to contain more than a dozen controls (including labels) it is almost always better to have button for that category that will launch a sub-form. This really helps to reduce the complexity of the main form.

我发现无论您需要多少控件,它们通常都可以分为不同的组。如果特定的组或类别将包含十几个控件(包括标签),则几乎总是最好为该类别设置按钮以启动子表单。这确实有助于降低主窗体的复杂性。

回答by TheCodeArtist

No hard n soft rule...

没有硬性和软性规则...



There is no definite number of controls that VBA will limit you to.
It is entirely dependent on the system you run it on.

VBA 将限制您使用的控件数量没有确定。
它完全取决于您运行它的系统。

As a general rule of thumb:

作为一般经验法则:

  • Reduce the number of controls.
  • Use grid controls instead of arrays of buttons.
  • Split it into logical forms for further simplicity.
  • Use lightweight controls(handle-less controls) as they consume less memory.
  • 减少控件的数量。
  • 使用网格控件而不是按钮阵列。
  • 将其拆分为逻辑形式以进一步简化。
  • 使用轻量级控件(无手柄控件),因为它们消耗更少的内存。

Apart from this, if you still are using more than 100 controls on the screen (as you say you are) then its time you hired a new UI designer for the project.

除此之外,如果您仍然在屏幕上使用超过 100 个控件(正如您所说的那样),那么是时候为该项目聘请新的 UI 设计师了。

GoodLUCK!!

祝你好运!!

PS: Do try and split the form up, if possible.
I can't imagine using any software that throws up 154 controls in one screen.
(MS-WORD comes pretty close ;-) )

PS:如果可能,请尝试拆分表格。
我无法想象使用任何可以在一个屏幕上显示 154 个控件的软件。
(MS-WORD 非常接近 ;-) )



UPDATE:Some stuff for your reference below...

更新:下面有一些供您参考的东西......

回答by guitarthrower

I try to stay away from userforms that are this big. I much prefer to use the spreadsheet to handle the data collection for things this big. If there are dependencies, then I hide/unhide rows as necessary, or use multiple sheets. I find it is an easier solution (sometimes).

我尽量远离这么大的用户表单。我更喜欢使用电子表格来处理这么大的事情的数据收集。如果存在依赖关系,那么我会根据需要隐藏/取消隐藏行,或者使用多个工作表。我发现这是一个更简单的解决方案(有时)。