Not enough memory crash when loading VBA userform

╄→гoц情女王★ 提交于 2019-11-30 16:16:34

I once fixed a crashing Excel form with many controls by making sure the code always refers all controls via the Controls collection:

Me.Controls("Cond1").Caption = .Cells(r, c + 1)

and never via their code names:

Me.Cond1.Caption = .Cells(r, c + 1)

It was weird, but it worked.
Try replacing all your Stb.Cond1.Caption with Stb.Controls("Cond1").Caption etc.


That was about 15 years ago, and up until today it remained unclear to me why it worked and why on Earth I even thought that utilising Controls in this way might have something to do with fixing it. As I learned today, it is a long standing limitation in Excel on the number of controls that can be addressed directly by their name - a poorly documented limitation, and a poorly implemented one too, as such a limitation must produce a compilation error rather than a runtime error at a random moment.

I can't speak to why your Excel is crashing, but you could probably cut down some code by loading some values into an Array, then instead of 6 For v = 0 to 28 loops you could have two nested loops, the inner one iterating through your array:

Dim start As Long, z As Long, arr As Variant

start = 1
arr = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9")

For v = 0 To 28
    For z = 0 To UBound(arr)
        If Len(WorksheetFunction.Substitute(.Cells(r, c + 3 + v), arr(z), "")) < Len(.Cells(r, c + 3 + v)) Then Controls("CheckBox" & start + v).Value = True
        start = start + 30
    Next z
    start = 1
Next 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!