Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default formatting text

Hello all!

I am writing a function to format text to be displayed in a MsgBox.
So far it works, however, it will cut off the last word in the last line if
the text is over 45 characters - and error out if there are no spaces in the
last line. - ie the text is 50 characters long and the 'remainder' is the
word "end.")

(Thanks to Tom O. for the base of this code)

what am i missing?

<BEGIN VBA CODE
Public Function formatString(str As String)

If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To CInt(Application.WorksheetFunction.RoundDown(Len(s tr) /
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function
<END VBA CODE
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default formatting text

changing
Do While Mid(temp,i,1) < " " And i 1

to

Do While Mid(temp,i,1) < " " And i 1 And Len(temp) 44

seems to have fixed the problem.

"Gixxer_J_97" wrote:

Hello all!

I am writing a function to format text to be displayed in a MsgBox.
So far it works, however, it will cut off the last word in the last line if
the text is over 45 characters - and error out if there are no spaces in the
last line. - ie the text is 50 characters long and the 'remainder' is the
word "end.")

(Thanks to Tom O. for the base of this code)

what am i missing?

<BEGIN VBA CODE
Public Function formatString(str As String)

If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To CInt(Application.WorksheetFunction.RoundDown(Len(s tr) /
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function
<END VBA CODE

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default formatting text

Small change, an extra test in the Do Loop

Public Function formatString(str As String)
Dim i, j
If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To CInt(Application.WorksheetFunction.RoundDown(Len(s tr) /
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1 And Len(temp) 45
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Gixxer_J_97" wrote in message
...
Hello all!

I am writing a function to format text to be displayed in a MsgBox.
So far it works, however, it will cut off the last word in the last line

if
the text is over 45 characters - and error out if there are no spaces in

the
last line. - ie the text is 50 characters long and the 'remainder' is the
word "end.")

(Thanks to Tom O. for the base of this code)

what am i missing?

<BEGIN VBA CODE
Public Function formatString(str As String)

If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To CInt(Application.WorksheetFunction.RoundDown(Len(s tr)

/
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function
<END VBA CODE



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default formatting text

just ooc, seeing as we just defined temp as a maximum of 45 characters, how
can len(temp) ever be greater than 45?

or did i miss the point - i tried using len(temp) 44 and that seems to
work perfectly.


"Bob Phillips" wrote:

Small change, an extra test in the Do Loop

Public Function formatString(str As String)
Dim i, j
If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To CInt(Application.WorksheetFunction.RoundDown(Len(s tr) /
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1 And Len(temp) 45
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Gixxer_J_97" wrote in message
...
Hello all!

I am writing a function to format text to be displayed in a MsgBox.
So far it works, however, it will cut off the last word in the last line

if
the text is over 45 characters - and error out if there are no spaces in

the
last line. - ie the text is 50 characters long and the 'remainder' is the
word "end.")

(Thanks to Tom O. for the base of this code)

what am i missing?

<BEGIN VBA CODE
Public Function formatString(str As String)

If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To CInt(Application.WorksheetFunction.RoundDown(Len(s tr)

/
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function
<END VBA CODE




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default formatting text

oops, I meant Len(str) 45

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Gixxer_J_97" wrote in message
...
just ooc, seeing as we just defined temp as a maximum of 45 characters,

how
can len(temp) ever be greater than 45?

or did i miss the point - i tried using len(temp) 44 and that seems to
work perfectly.


"Bob Phillips" wrote:

Small change, an extra test in the Do Loop

Public Function formatString(str As String)
Dim i, j
If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To

CInt(Application.WorksheetFunction.RoundDown(Len(s tr) /
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1 And Len(temp) 45
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Gixxer_J_97" wrote in message
...
Hello all!

I am writing a function to format text to be displayed in a MsgBox.
So far it works, however, it will cut off the last word in the last

line
if
the text is over 45 characters - and error out if there are no spaces

in
the
last line. - ie the text is 50 characters long and the 'remainder' is

the
word "end.")

(Thanks to Tom O. for the base of this code)

what am i missing?

<BEGIN VBA CODE
Public Function formatString(str As String)

If Len(str) <= 45 Then
formatString = str
Else
Dim temp As String
formatString = ""
For j = 0 To

CInt(Application.WorksheetFunction.RoundDown(Len(s tr)
/
45, 0))
temp = Left(str, 45)
i = 45
Do While Mid(temp, i, 1) < " " And i 1
i = i - 1
Loop
formatString = formatString + Mid(temp, 1, i) + Chr(13)
str = Mid(str, i + 1)
Next j
End If

End Function
<END VBA CODE








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
Formatting Text in cells that already have text entered Jennifer V. Excel Worksheet Functions 4 January 13th 09 09:27 PM
copying text within a text box and losing formatting hokiefan1us Excel Discussion (Misc queries) 2 September 17th 08 08:41 PM
Formula Text String: Formatting Text and Numbers? dj479794 Excel Discussion (Misc queries) 5 June 30th 07 12:19 AM
Conditional Formatting based on text within a cell w/ text AND num Shirley Excel Worksheet Functions 2 December 22nd 06 01:40 AM
Conditional Formatting based on Text within Text George Lynch Excel Discussion (Misc queries) 3 May 5th 05 07:58 PM


All times are GMT +1. The time now is 07:47 AM.

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"