Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default trim not working

I'm having some difficulty making the trim function work.

I pull some data from a server and occassionally that data will be
padded with extra characters at the end of the string, including both
chr(32) and chr(160). I have successfully removed the chr(160) using
the replace fucntion:

variable = replace(variable,chr(160),"")

and I'm trying to use trim to remove the spaces:

variable = trim(variable)

but the space remains.

I have tried rtrim (the padding always seems to be at the end of the
string) and I also tried the following:

variable = trim(str(variable))

and with this line of code I get a "type mismatch" error message, so
I'm thinking that my data is not appearing to be a string.

Do I need to fomat the cells where this data is loaded as text.

I'm at my wits end trying to figure this out, please share your
experience!


John Keith

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default trim not working

John,

VBA trim only removes leading and trailing spaces. Application.Worksheetfunction.Trim (or just
Application.Trim) will remove extra spaces internal to the string.

variable = Application.Trim(variable)

HTH,
Bernie
MS Excel MVP


"John Keith" wrote in message ...
I'm having some difficulty making the trim function work.

I pull some data from a server and occassionally that data will be
padded with extra characters at the end of the string, including both
chr(32) and chr(160). I have successfully removed the chr(160) using
the replace fucntion:

variable = replace(variable,chr(160),"")

and I'm trying to use trim to remove the spaces:

variable = trim(variable)

but the space remains.

I have tried rtrim (the padding always seems to be at the end of the
string) and I also tried the following:

variable = trim(str(variable))

and with this line of code I get a "type mismatch" error message, so
I'm thinking that my data is not appearing to be a string.

Do I need to fomat the cells where this data is loaded as text.

I'm at my wits end trying to figure this out, please share your
experience!


John Keith



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default trim not working

Bernie,

Thanks for responding. I was only wanting to remove spaces padded at
the end of a string so Trim is going to work fine for me.

I did find my problem (and I've decided that when I work late at night
I'm not as good at diagnosing issues).

My code statements were more complex than the samples in my OP and I
was trying to do multiple operations in one statement and that caused
some confusion. It turns out that I was doing the Replace and the Trim
in the wrong order. If Trim is done first and if the last character of
the string is chr(160) nothing is changed in the string and since Trim
does not act on chr(160) then Trim results in no change.




John Keith

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
Trim and Substitute not working Fuzzy Excel Worksheet Functions 4 December 23rd 09 12:01 PM
Excel 2002 : Why =Trim () formula not working ? Mr. Low Excel Discussion (Misc queries) 6 August 23rd 07 01:52 PM
Trim not working.... FlatEric Excel Programming 2 February 21st 06 01:30 PM
Excel trim not working Kharpo7 Excel Discussion (Misc queries) 5 June 21st 05 12:53 AM
Trim not working Stuart[_21_] Excel Programming 5 May 17th 05 07:55 PM


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