View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default extracting characters at a specified limit to seperate cells

On 22 Jan 2007 09:26:37 -0800, "tevid" wrote:

Ron I would like to then extract the remaining characters into another
cell


And what about having the break at a "logical point" (e.g. after the tilde?).

If the location of the break is not important, you can just extend Dave's
formula, perhaps with an error check when the formula returns a null string:

e.g. =IF(LEN(MID(A1,300,75))0,MID(A1,300,75),"")

If you want the break to be at the "tilde", then one way is using VBA to write
a User Defined Function using Regular Expressions.

For that:

<alt-F11 opens the VB Editor.
Ensure your project is highlighted in the Project Explorer window.
Insert/Module and paste the code below into the window that opens.
From the menu bar of the VB Editor: Tools/References and select "Microsoft
VBScript Regular Expressions 5.5" from the list of Available References.

Then, with your string in A1, enter this formula in some cell and drag across
as far (or further) than required to return the entire string:

=remid($A$1,"[\s\S]{1,74}~",COLUMNS($A:A))

================================================== ==========
Option Explicit

Function REMid(str As String, Pattern As String, _
Optional Index As Variant = 1, _
Optional CaseSensitive As Boolean = True) _
As Variant 'Variant as value may be string or array

Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection

Dim i As Long 'counter

' Create a regular expression object.
Set objRegExp = New RegExp

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Test whether the String can be compared.
If (objRegExp.Test(str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(str) ' Execute search.

On Error Resume Next 'return null string if a colmatch index is non-existent
If IsArray(Index) Then
ReDim t(1 To UBound(Index))
For i = 1 To UBound(Index)
t(i) = colMatches(Index(i) - 1)
Next i
REMid = t()
Else
REMid = CStr(colMatches(Index - 1))
If IsEmpty(REMid) Then REMid = ""
End If
On Error GoTo 0 'reset error handler
Else
REMid = ""
End If
End Function

======================================
--ron