Results 1 to 20 of 37

Thread: Optimize String Handling in Visual Basic

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Was chatting with Jake on YIM here, and I ran up a longer test than he did, and came up with some different results. Ain't as perty and you have to add your own average formula right now, but:

    Sub b()
        Dim MyTimer     As Double
        Dim i           As Long
        Dim j           As Long
        Application.ScreenUpdating = False
        Range("C1").Value = "vbnullstring"
        Range("D1").Value = "2 quotes"
        For j = 1 To 50
            Range("A:A").ClearContents
            MyTimer = Timer
            For i = 1 To 10000
                If Range("A" & i).Value = vbNullString Then
                    Range("A" & i).Value = 1
                End If
            Next i
            Range("C65536").End(xlUp).Offset(1, 0).Value = Timer - MyTimer
        Next j
        For j = 1 To 50
            Range("A:A").ClearContents
            MyTimer = Timer
            For i = 1 To 10000
                If Range("A" & i).Value = "" Then
                    Range("A" & i).Value = 1
                End If
            Next i
            Range("D65536").End(xlUp).Offset(1, 0).Value = Timer - MyTimer
        Next j
    End Sub
    For me, after 50 loops, vbnullstring = .8225, "" = 1.51838

    The weird part is that while vbnullstring starts and stays fast, "" seems to slow down after a few iterations.
    Last edited by Aussiebear; 04-10-2025 at 07:02 PM.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •