View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Mark Lincoln Mark Lincoln is offline
external usenet poster
 
Posts: 227
Default String manipulation in macro runs slower each time ran.

I don't want to open unknown files except as a last resort. Let's see
if we can solve this first.

We're thinking the same way in that we believe the continual shifting
down of cells is causing the problem.

Do you *have* to shift cells? Can you just move excess text to the
next cell down until you've finished? I worked up an example that
works this way. But I only tested it with short strings and chopped
them up into 10-character lengths. If it will work for you without
hosing the rest of your workbook, modify it as needed to fit your
situation and see if it keeps Excel from getting cranky.

Option Explicit

Sub ChopText()
'Assumes the user has activated
'the cell containing the text
'to be manipulated

Dim R As Integer
Dim C As Integer
Dim RemainingLength As Integer
Dim ExcessText As String

R = ActiveCell.Row
C = ActiveCell.Column

Do
If R 10000 Then Exit Do
If Len(Cells(R, C).Value) 255 Then
ExcessText = Right(Cells(R, C).Value, Len(Cells(R, C)) - 255)
Cells(R, C).Value = Left(Cells(R, C).Value, 255)
R = R + 1
Cells(R, C).Value = ExcessText
RemainingLength = Len(ExcessText)
End If
Loop Until RemainingLength <= 255

End Sub