vba - Excel UDF: use Characters object without ScreenUpdating - Stack Overflow

It appears that if an Excel VBA user-defined function makes use of a Characters object, the cell in whi

It appears that if an Excel VBA user-defined function makes use of a Characters object, the cell in which that function is used displays some part of that object while the VBA code is running, even if ScreenUpdating is turned off. This seriously slows down the execution. How can this be turned off or worked around?

Here is a post on Microsoft's forum where someone apparently has the same problem.

Here is a sample user-defined function to demonstrate the problem. I have repeated one line many times in order to make the effect more easily visible.

Public Function Foobar(theCell As Range) As String

    Dim i As Integer
    Dim Result As String
    
    Application.ScreenUpdating = False 'this has no effect
    
    For i = 1 To theCell.Characters.Count
        Result = theCell.Characters(i, 1).Text
        'repeat more times so the effect remains visible onscreen
        Result = theCell.Characters(i, 1).Text
        Result = theCell.Characters(i, 1).Text
        Result = theCell.Characters(i, 1).Text
        '... repeat ~10 more times
    Next i
    
    Foobar = Result
    
    Application.ScreenUpdating = True

End Function

Fill cell A1 with ~500 characters of random text and put =Foobar(A1) in cell B1. Hit enter and you see a portion of that text temporarily displayed in cell B1. The text remains there until calculation finishes and the result is displayed. This happens whether or not ScreenUpdating has been disabled.

Here is a screenshot made while the calculation was running.

What is the reason for this behavior? Is it correct to blame the slowness on VBA interfacing with Excel's display? Is there a way to bypass it?

If it is relevant, I'm using Excel 2013.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744061675a4551890.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信