Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default one very, very bad line of code? Trim and chr(10)

I wanted to test to see if the Trim function would also take off any extra
chr(10)'s because I'm concatenating strings within cells and have to line up
my information with adjacent cells- but I sometimes end up with an extra
chr(10) at either the beginning or end of the cell, and I was looking for an
easy way to remove it.

I wrote the following to test to see if Trim would work (or if it only works
on regular extra spaces). The first several times I got an 'out of memory'
error, so I rebooted, and from a clean boot, tried again, and it ended up
taking over by hogging loads of memory -at least I don't have any other
explanation, since nothing else was running on the PC, and when I hard
exited Excel via the task manager my memory opened back up.

So, here's my question- why would this one line of code cause Excel to
"freak out"? Or do I have something else going on that I'm mis-attributing
to Excel? XL2003 on Win2000.

Test at your own risk :\

Sub testTrim()
Sheet4.Range("B2").Value = Trim(Str("hhh" & Chr(10)))
End Sub


--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default one very, very bad line of code? Trim and chr(10)

Hi KR,

KR wrote:
I wanted to test to see if the Trim function would also take off any
extra chr(10)'s because I'm concatenating strings within cells and
have to line up my information with adjacent cells- but I sometimes
end up with an extra chr(10) at either the beginning or end of the
cell, and I was looking for an easy way to remove it.


Sub testTrim()
Sheet4.Range("B2").Value = Trim(Str("hhh" & Chr(10)))
End Sub


That works fine for me once I get it to compile (had to either change Str to
CStr or remove that function altogether else I get a type mismatch error).

That said, Trim() will not remove anything but trailing and leading spaces.
You can use the Replace() function to do this:

Sheet4.Range("B2").Value = Replace("hhh" & Chr(10), Chr(10),
vbNullString)

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default one very, very bad line of code? Trim and chr(10)

Just caused an error for me since str is looking for a number argument to
convert to a string

changing it to cstr fixed it. But you don't need any conversion at all
since it is a string already

Sub testTrim()
Sheet3.Range("B2").Value = Trim(CStr("hhh" & Chr(10)))
End Sub


Sub testTrim()
Sheet3.Range("B2").Value = Trim("hhh" & Chr(10))
End Sub

--
Regards,
Tom Ogilvy

"KR" wrote in message
...
I wanted to test to see if the Trim function would also take off any extra
chr(10)'s because I'm concatenating strings within cells and have to line

up
my information with adjacent cells- but I sometimes end up with an extra
chr(10) at either the beginning or end of the cell, and I was looking for

an
easy way to remove it.

I wrote the following to test to see if Trim would work (or if it only

works
on regular extra spaces). The first several times I got an 'out of memory'
error, so I rebooted, and from a clean boot, tried again, and it ended up
taking over by hogging loads of memory -at least I don't have any other
explanation, since nothing else was running on the PC, and when I hard
exited Excel via the task manager my memory opened back up.

So, here's my question- why would this one line of code cause Excel to
"freak out"? Or do I have something else going on that I'm mis-attributing
to Excel? XL2003 on Win2000.

Test at your own risk :\

Sub testTrim()
Sheet4.Range("B2").Value = Trim(Str("hhh" & Chr(10)))
End Sub


--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.




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
Macro code to put series name next to individual line in line grap Otani Charts and Charting in Excel 3 February 23rd 10 07:24 PM
Trim with Country code leading Cassius Excel Worksheet Functions 3 June 9th 09 06:54 AM
Code for Trim function shaji Excel Discussion (Misc queries) 2 June 15th 06 09:38 AM
Trim function doesn't clean out ASCII Code 160 (Space) Ronald Dodge Excel Worksheet Functions 6 January 27th 05 03:48 AM
VBA Trim and Application.worksheetfunction.Trim Hari Prasadh Excel Programming 3 January 19th 05 02:22 PM


All times are GMT +1. The time now is 05:38 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"