Question : Array to Range crash Excel bug

Points awarded if someone knows about a way to fix this.  Otherwise, I do not see this bug reported anywhere, so it might be an interesting conversation.  I have a workaround - if any of the elements are longer than 900 characters, then each one is idividually written to the worksheet...

'How to crash Excel - copy an array to a range.
'The array must have at least two elements in it.
'One of the elements must have 913 or more characters in it.
'Microsoft and others have noted that if many values are to be transferred
'to a worksheet, then the fastest way is to copy an array to a range.  For
'example, see
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexvba/html/odc_5709_chap05idx.asp
'
'"Transferring Values between Arrays and Ranges"
'
'In the process of implementing this practice, I encountered a fatal bug

Public Sub ArrayCrash()

    'Array to Range Excel crash demo
    'if one of the array elements contains a
    'string of length 913 or greater, then
    'Excel will crash
   
    'tested in Excel 97
    'failures were generated, but not fully tested, in Excel 2002

    Dim ws As Worksheet
    Dim TstArray As Variant
    Dim txt As String

    'array must have more than one element,
    'else error is not triggered
    'it does not matter if array is variant or string
    'it does not matter if ReDim is used, or if array bounds
    'are defined in the Dim statement
    ReDim TstArray(1 To 1, 1 To 2)
   
    txt = ""
    txt = txt & String(100, "a")
    txt = txt & String(100, "b")
    txt = txt & String(100, "c")
    txt = txt & String(100, "d")
    txt = txt & String(100, "e")
    txt = txt & String(100, "f")
    txt = txt & String(100, "g")
    txt = txt & String(100, "h")
    txt = txt & String(100, "i")
   
    'a length of 12 works, 13 fails
    txt = txt & String(13, "k")
   
    TstArray(1, 1) = txt
   
    Set ws = ActiveSheet
   
    'either range statement will cause failure
'    ws.Range(ws.Cells(5, 1), ws.Cells(5, 2)) = TstArray
    ws.Range("A5:B5") = TstArray

End Sub


Answer : Array to Range crash Excel bug

Just ran your code on Excel XP SP-2 without a problem.
Random Solutions  
 
programming4us programming4us