Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 9
Default Formatting string in groups of letters...

Hi,

How can a string without spaces be formatted to display the string in groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters

--
Håkan


  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,646
Default Formatting string in groups of letters...

=LEFT(A2,LEN(A2)-15)&" "&MID(A2,LEN(A2)-14,5)&" "&MID(A2,LEN(A2)-9,5)&"
"&RIGHT(A2,5)

Regards,
Stefi

€žHBj€ ezt Ã*rta:

Hi,

How can a string without spaces be formatted to display the string in groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters

--
HÃ¥kan



  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 9
Default Formatting string in groups of letters...

Sorry, you misunderstood my problem. I mean only the DISPLAY FORMAT (in the
same manner as thousand separator). The string is physically untouched, only
the way it is displayed is the problem.

Also, the string length may vary from 3 characters to about 40.

Håkan
"Stefi" wrote in message
...
=LEFT(A2,LEN(A2)-15)&" "&MID(A2,LEN(A2)-14,5)&" "&MID(A2,LEN(A2)-9,5)&"
"&RIGHT(A2,5)

Regards,
Stefi

"HBj" ezt írta:

Hi,

How can a string without spaces be formatted to display the string in
groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters

--
Håkan





  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 8,651
Default Formatting string in groups of letters...

A string is not affected by display formats.
--
David Biddulph

"HBj" wrote in message
...
Sorry, you misunderstood my problem. I mean only the DISPLAY FORMAT (in
the same manner as thousand separator). The string is physically
untouched, only the way it is displayed is the problem.

Also, the string length may vary from 3 characters to about 40.

Håkan
"Stefi" wrote in message
...
=LEFT(A2,LEN(A2)-15)&" "&MID(A2,LEN(A2)-14,5)&" "&MID(A2,LEN(A2)-9,5)&"
"&RIGHT(A2,5)

Regards,
Stefi

"HBj" ezt írta:

Hi,

How can a string without spaces be formatted to display the string in
groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters

--
Håkan







  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,646
Default Formatting string in groups of letters...

I understood your request, but I forgot to mention - like David didi it -
that strings cannot be formatted. I suggested a workaround, and here is a
modified solution to handle strings up to either 40 characater length:

=RIGHT(LEFT($A$2,LEN($A$2)-15),5)&" "&RIGHT(LEFT($A$2,LEN($A$2)-10),5)&"
"&RIGHT(LEFT($A$2,LEN($A$2)-5),5)&" "&RIGHT(A2,5)

This is good up to 20 character length, if you want to expand length to 25,
insert
RIGHT(LEFT($A$2,LEN($A$2)-20),5)&" "
at the beginning of the formula, and so on.

You can achieve an effect as if the string would be formatted, if you hide
the column containing the source strings and use the above formula in the
visible adjacent column.

Regards,
Stefi

€žHBj€ ezt Ã*rta:

Sorry, you misunderstood my problem. I mean only the DISPLAY FORMAT (in the
same manner as thousand separator). The string is physically untouched, only
the way it is displayed is the problem.

Also, the string length may vary from 3 characters to about 40.

HÃ¥kan
"Stefi" wrote in message
...
=LEFT(A2,LEN(A2)-15)&" "&MID(A2,LEN(A2)-14,5)&" "&MID(A2,LEN(A2)-9,5)&"
"&RIGHT(A2,5)

Regards,
Stefi

"HBj" ezt Ã*rta:

Hi,

How can a string without spaces be formatted to display the string in
groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters

--
HÃ¥kan








  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 9
Default Formatting string in groups of letters...

I have tried with and expected "Format-Cells-Custom" etc. with some kind
of pattern. It's no problem to _modify_ the string to show what you want,
but it requires one more column and confusion when you need to change it
back to its original format.
So, it seems to me that I do better make a short VBA snip to add the spaces
for displaying it and a second to compress.

Rgds
Håkan

"Stefi" wrote in message
...
I understood your request, but I forgot to mention - like David didi it -
that strings cannot be formatted. I suggested a workaround, and here is a
modified solution to handle strings up to either 40 characater length:

=RIGHT(LEFT($A$2,LEN($A$2)-15),5)&" "&RIGHT(LEFT($A$2,LEN($A$2)-10),5)&"
"&RIGHT(LEFT($A$2,LEN($A$2)-5),5)&" "&RIGHT(A2,5)

This is good up to 20 character length, if you want to expand length to
25,
insert
RIGHT(LEFT($A$2,LEN($A$2)-20),5)&" "
at the beginning of the formula, and so on.

You can achieve an effect as if the string would be formatted, if you hide
the column containing the source strings and use the above formula in the
visible adjacent column.

Regards,
Stefi

"HBj" ezt írta:

Sorry, you misunderstood my problem. I mean only the DISPLAY FORMAT (in
the
same manner as thousand separator). The string is physically untouched,
only
the way it is displayed is the problem.

Also, the string length may vary from 3 characters to about 40.

Håkan
"Stefi" wrote in message
...
=LEFT(A2,LEN(A2)-15)&" "&MID(A2,LEN(A2)-14,5)&" "&MID(A2,LEN(A2)-9,5)&"
"&RIGHT(A2,5)

Regards,
Stefi

"HBj" ezt írta:

Hi,

How can a string without spaces be formatted to display the string in
groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters

--
Håkan








  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default Formatting string in groups of letters...

On Thu, 5 Mar 2009 13:49:43 +0200, "HBj" wrote:

Hi,

How can a string without spaces be formatted to display the string in groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters


You cannot accomplish this with formatting. The best you can do, while leaving
the original string unchanged, is to display it in the desired fashion in
another cell.

Here is a UDF (user-defined-function) that will take a string as input, remove
any existing spaces, and then output a string spaced according to your rules.

The default spacing is five characters, but optional arguments allow you to
specify any desired number of characters, and also to specify whether the
formatted string fills from the right (the default) or from the left (so that
the right-most group could contain less than L characters).

To enter this User Defined Function (UDF), <alt-F11 opens the Visual Basic
Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

=InsertSpaces(A1)

in some cell.


=================================================
Option Explicit
Function InsertSpaces(sStr, Optional Length As Long = 5, _
Optional RightToLeft As Boolean = True)
Dim sFormat As String
Dim sTemp As String
sTemp = Replace(sStr, " ", "")

With Application.WorksheetFunction
sFormat = " " & .Rept("@", Length)
sFormat = .Rept(sFormat, .Ceiling(Len(sTemp) / Length, 1))
End With

If RightToLeft = False Then
sFormat = "!" & sFormat
End If

InsertSpaces = Trim(Format(sTemp, sFormat))

End Function
========================================



If you want to use the optional arguments, you could do that by entering a
formula of the type:

=InsertSpaces(A1,[num_spaces],[fill_direction])

for [fill_direction] -- FALSE will fill left-to-right.

--ron
  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 9
Default Formatting string in groups of letters...

Thank you Ron for your complete answer. Looks like you wrote this code for
this very case.

Håkan

"Ron Rosenfeld" wrote in message
...
On Thu, 5 Mar 2009 13:49:43 +0200, "HBj"
wrote:

Hi,

How can a string without spaces be formatted to display the string in
groups
of five characters.
Example:

246 81357 99876 54321

Only the left-most group can contain less than 5 characters


You cannot accomplish this with formatting. The best you can do, while
leaving
the original string unchanged, is to display it in the desired fashion in
another cell.

Here is a UDF (user-defined-function) that will take a string as input,
remove
any existing spaces, and then output a string spaced according to your
rules.

The default spacing is five characters, but optional arguments allow you
to
specify any desired number of characters, and also to specify whether the
formatted string fills from the right (the default) or from the left (so
that
the right-most group could contain less than L characters).

To enter this User Defined Function (UDF), <alt-F11 opens the Visual
Basic
Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

=InsertSpaces(A1)

in some cell.


=================================================
Option Explicit
Function InsertSpaces(sStr, Optional Length As Long = 5, _
Optional RightToLeft As Boolean = True)
Dim sFormat As String
Dim sTemp As String
sTemp = Replace(sStr, " ", "")

With Application.WorksheetFunction
sFormat = " " & .Rept("@", Length)
sFormat = .Rept(sFormat, .Ceiling(Len(sTemp) / Length, 1))
End With

If RightToLeft = False Then
sFormat = "!" & sFormat
End If

InsertSpaces = Trim(Format(sTemp, sFormat))

End Function
========================================



If you want to use the optional arguments, you could do that by entering a
formula of the type:

=InsertSpaces(A1,[num_spaces],[fill_direction])

for [fill_direction] -- FALSE will fill left-to-right.

--ron



  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default Formatting string in groups of letters...

On Fri, 6 Mar 2009 15:55:24 +0200, "HBj" wrote:

Thank you Ron for your complete answer.


You're welcome. Glad to help.

Looks like you wrote this code for
this very case.


I did!
--ron
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
Breaking a string of letters Confused Excel Discussion (Misc queries) 2 March 10th 07 11:49 AM
Breaking a string of letters john Excel Discussion (Misc queries) 0 March 8th 07 07:31 PM
Converting string of letters to Number sebkavam Excel Discussion (Misc queries) 6 July 26th 06 10:50 PM
count number of letters in a string Wiley Excel Worksheet Functions 3 May 11th 06 06:54 PM
Counting groups of exact case numbers w/letters in them. tjtjjtjt Excel Discussion (Misc queries) 2 November 25th 04 08:13 PM


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

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

About Us

"It's about Microsoft Excel"