View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Cannot write the contents of an Object to file

I wouldn't worry about 26 lines but over a few thousand and the speed can
slow exponentially. This should give you quite a dramatic illustration

Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Sub test()
Dim i As Long, pos As Long, nLen As Long, t As Long
Dim s1 As String, s2 As String
Const C As Long = (20000 - 1)

ReDim arr(0 To C) As String
' build a test array
For i = LBound(arr) To UBound(arr)
Select Case i Mod 4
Case 0: arr(i) = "a"
Case 1: arr(i) = "bb"
Case 2: arr(i) = "ccc"
Case 3: arr(i) = "dddd"
End Select
Next

'' padded Mid way
t = GetTickCount
For i = LBound(arr) To UBound(arr)
nLen = nLen + Len(arr(i))
Next

' add +2 for each vbCrLF
nLen = nLen + (UBound(arr) - LBound(arr)) * 2

s1 = Space(nLen) ' size the string
pos = 1
For i = LBound(arr) To UBound(arr)
Mid$(s1, pos, Len(arr(i))) = arr(i)
pos = pos + Len(arr(i))
If i < UBound(arr) Then
Mid$(s1, pos, 2) = vbCrLf
pos = pos + 2
End If
Next
Debug.Print Len(s1), (GetTickCount - t) / 1000

' now the simple way
t = GetTickCount
For i = LBound(arr) To UBound(arr)
s2 = s2 & arr(i)
If i < UBound(arr) Then
s2 = s2 & vbCrLf
End If
Next
Debug.Print Len(s2), (GetTickCount - t) / 1000

End Sub

Regards,
Peter T


"deltaquattro" wrote in message
...
On 26 Apr, 18:46, "Peter T" <peter_t@discussions wrote:
It sounds like you've got the idea!


:) yes, thanks to your very clear examples.

One thing though, if building a very large string, say by looping a large
array, size the string first, s=Space(nSize) then pad it with the Mid
function (don't forget to include 2*(loops-1) for the vbCrLF characters.
Ask
if not sure how (but only if building a long string in many loops).


The array in my real code can indeed be large (in the example I fixed
the number of lines to 26 but it can be much more). What do you mean
by padding with Mid function? Could you write an example? Thanks,

Best Regards

deltaquattro