View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
GS[_5_] GS[_5_] is offline
external usenet poster
 
Posts: 226
Default Moving extra characters to a new column

Ron Rosenfeld was thinking very hard :

And if there are more than 255*2 characters?

This will just fill to the right with a maximum of 255 characters per cell.

You'll need to ensure the target range is clear.

=======================
Option Explicit
Sub Max255Chars()
Dim rg As Range, c As Range
Dim L As Long
Dim S As String
Set rg = Selection
For Each c In Selection
S = c.Text
For L = 1 To Len(S) Step 255
c(1, L \ 255 + 1).Value = Mid(S, L, 255)
Next L
Next c
End Sub
==========================


Geez Ron, that's really nice! I was working on a recursive routine when
I saw your post. I like your idea of using a loop better, but what's
with declaring/setting rg and not using it?

Here's where I was going...

Sub MaxCellChars255()
' Parses cell contents to 255[Max} characters;
' Moves excess characters to adjacent cell[s] to right.
' Recursive: Will use as many cells as required.

Dim sTemp As String, c As Range, lPos As Long

For Each c In Selection
lPos = 0
If Not IsEmpty(c) And Len(c) 255 Then
sTemp = c: c = Left$(sTemp, 255)
recheck:
sTemp = Mid$(sTemp, 256): lPos = lPos + 1
c.Offset(0, lPos) = Left$(sTemp, 255)
If Len(sTemp) 255 Then GoTo recheck
End If
Next
End Sub

While it's more self-documenting than yours, it's not as efficient.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc