Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Split field based on number of characters and space


I have a string of text in a cell. I want to split the cell based on
anything over 30 characters. But I want to also split on a space. So
basically the first space before I reach 30 characters limit. Hope I
explained well !! Any help would be great !

Thanks


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile: http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Split field based on number of characters and space

s = cell.Value
if len(s) < 30 then exit sub
iloc = Instr(1,s," ",vbTextCompare)
if iloc < 30 and iloc < 0 then
s1 = left(s,iloc-1)
s2 = mid(s,iloc+1,255)
else
s1 = left(s,30)
s2 = Mid(s,31,255)
end if

--
Regards,
Tom Ogilvy

"william_mailer"
<william_mailer.22x0oz_1139409913.2219@excelforu m-nospam.com wrote in
message news:william_mailer.22x0oz_1139409913.2219@excelfo rum-nospam.com...

I have a string of text in a cell. I want to split the cell based on
anything over 30 characters. But I want to also split on a space. So
basically the first space before I reach 30 characters limit. Hope I
explained well !! Any help would be great !

Thanks


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile:

http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Split field based on number of characters and space


Hi There,

Thanks for the input but i cant seem to get it working !? I've copied
the code into a function and referred to it on the spreadsheet, but its
just returning #value.......Any idea what i might be doing wrong ?


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile: http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Split field based on number of characters and space

Public Function SplitString(rng As Range)
Dim s As String
s = rng.Value
If Len(s) < 30 Then
SplitString = s
Else
iloc = InStr(1, s, " ", vbTextCompare)
If iloc < 30 And iloc < 0 Then
S1 = Left(s, iloc - 1)
s2 = Mid(s, iloc + 1, 255)
SplitString = S1
Else
S1 = Left(s, 30)
s2 = Mid(s, 31, 255)
SplitString = S1
End If
End If

End Function

usage (assume in B1
=SplitString(A1)

to get the right side

=Right(A1,len(A1)-len(B1))

--
Regards,
Tom Ogilvy

"william_mailer"
<william_mailer.22x2bb_1139412001.6385@excelforu m-nospam.com wrote in
message news:william_mailer.22x2bb_1139412001.6385@excelfo rum-nospam.com...

Hi There,

Thanks for the input but i cant seem to get it working !? I've copied
the code into a function and referred to it on the spreadsheet, but its
just returning #value.......Any idea what i might be doing wrong ?


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile:

http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Split field based on number of characters and space


My explanation was crap, i do apologise !!

If the string is longer than 30 characters, I need the text before the
last space before the 30 character limit.........

Example:

String: This is a massively long string of text and I want to split it

Result1: This is a massively long
Result2: string of text and I want to split it

Note: If I split at 30 characters I break a word so I want to split at
the previous space !

Hope this makes sense !


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile: http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Split field based on number of characters and space

Public Function SplitString(rng As Range)
Dim s As String
s = Left(rng.Value, 30)
If Len(s) < 30 Then
SplitString = s
Else
iloc = InStrRev(s, " ", -1, vbTextCompare)
If iloc < 0 Then
S1 = Left(s, iloc - 1)
s2 = Mid(s, iloc + 1, 255)
SplitString = S1
Else
SplitString = s
End If
End If

End Function


--
Regards,
Tom Ogilvy

"william_mailer"
<william_mailer.22x6xy_1139418004.0799@excelforu m-nospam.com wrote in
message news:william_mailer.22x6xy_1139418004.0799@excelfo rum-nospam.com...

My explanation was crap, i do apologise !!

If the string is longer than 30 characters, I need the text before the
last space before the 30 character limit.........

Example:

String: This is a massively long string of text and I want to split it

Result1: This is a massively long
Result2: string of text and I want to split it

Note: If I split at 30 characters I break a word so I want to split at
the previous space !

Hope this makes sense !


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile:

http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Split field based on number of characters and space


Tom,

Thats perfect, thanks a million for your help !


--
william_mailer
------------------------------------------------------------------------
william_mailer's Profile: http://www.excelforum.com/member.php...o&userid=31318
View this thread: http://www.excelforum.com/showthread...hreadid=510057

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Rows based on a number in a field Yookaroo Excel Worksheet Functions 2 June 26th 09 10:46 PM
return value based on number of characters in cell / field Perplexed Excel Worksheet Functions 4 December 10th 08 08:48 PM
LIMIT THE NUMBER OF CHARACTERS ENTERED IN A FIELD RichN Excel Worksheet Functions 2 November 14th 08 07:53 PM
Deleting rows based on number of characters catalfamo1220 Excel Discussion (Misc queries) 3 July 20th 06 06:31 PM
Split field based on number of characters and space william_mailer Excel Worksheet Functions 6 February 10th 06 01:26 AM


All times are GMT +1. The time now is 03:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"