Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Add Trim Function to Code

I have a sub that filters, selects cells and pastes them (transposed) as
headers in another area of the worksheet. I'd like to then trim the extra
spaces from the newly pasted headings. I'm very familiar with the Trim
function in Excel, but would like to to it in my sub. I've tried adding the
following code to my sub but, while it doesn't generate any error messages,
it just doesn't work:

-----Beginning of Code......
With Selection.(Formatting, etc.)
End With
Selection.SpecialCells(xlCellTypeConstants, 23).Select
------Then I add the following:

Dim cell

' Find all the cells in the current selection.
For Each cell In Selection
'This code repeats once for each cell in the selection.
cell.Value = Application.WorksheetFunction.Trim(cell.Value)
Next

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Add Trim Function to Code

Your posted code works (removes leading and following and extra internal
spaces).

Make sure Selection is good. Just before the For loop put:

MsgBox(Selection.Address)
--
Gary''s Student - gsnu200905


"Joyce" wrote:

I have a sub that filters, selects cells and pastes them (transposed) as
headers in another area of the worksheet. I'd like to then trim the extra
spaces from the newly pasted headings. I'm very familiar with the Trim
function in Excel, but would like to to it in my sub. I've tried adding the
following code to my sub but, while it doesn't generate any error messages,
it just doesn't work:

-----Beginning of Code......
With Selection.(Formatting, etc.)
End With
Selection.SpecialCells(xlCellTypeConstants, 23).Select
------Then I add the following:

Dim cell

' Find all the cells in the current selection.
For Each cell In Selection
'This code repeats once for each cell in the selection.
cell.Value = Application.WorksheetFunction.Trim(cell.Value)
Next

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Add Trim Function to Code

Hi there,

Thanks for your response.

I tested it as you recommended and the code is working. It seems to be the
actual cell content that doesn't trim. I tried it with the regular trim
function in Excel with the same result.

The data was imported, so perhaps there's something not apparent to the eye.

Back to the drawing board.

"Gary''s Student" wrote:

Your posted code works (removes leading and following and extra internal
spaces).

Make sure Selection is good. Just before the For loop put:

MsgBox(Selection.Address)
--
Gary''s Student - gsnu200905


"Joyce" wrote:

I have a sub that filters, selects cells and pastes them (transposed) as
headers in another area of the worksheet. I'd like to then trim the extra
spaces from the newly pasted headings. I'm very familiar with the Trim
function in Excel, but would like to to it in my sub. I've tried adding the
following code to my sub but, while it doesn't generate any error messages,
it just doesn't work:

-----Beginning of Code......
With Selection.(Formatting, etc.)
End With
Selection.SpecialCells(xlCellTypeConstants, 23).Select
------Then I add the following:

Dim cell

' Find all the cells in the current selection.
For Each cell In Selection
'This code repeats once for each cell in the selection.
cell.Value = Application.WorksheetFunction.Trim(cell.Value)
Next

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Add Trim Function to Code

On Sep 29, 6:31*pm, Joyce wrote:
Hi there,

Thanks for your response.

I tested it as you recommended and the code is working. *It seems to be the
actual cell content that doesn't trim. *I tried it with the regular trim
function in Excel with the same result.

The data was imported, so perhaps there's something not apparent to the eye.

Back to the drawing board.



"Gary''s Student" wrote:
Your posted code works (removes leading and following and extra internal
spaces).


Make sure Selection is good. *Just before the For loop put:


MsgBox(Selection.Address)
--
Gary''s Student - gsnu200905


"Joyce" wrote:


I have a sub that filters, selects cells and pastes them (transposed) as
headers in another area of the worksheet. *I'd like to then trim the extra
spaces from the newly pasted headings. *I'm very familiar with the Trim
function in Excel, but would like to to it in my sub. *I've tried adding the
following code to my sub but, while it doesn't generate any error messages,
it just doesn't work:


-----Beginning of Code......
* *With Selection.(Formatting, etc.)
* * End With
* * * Selection.SpecialCells(xlCellTypeConstants, 23).Select
* ------Then I add the following:


* * Dim cell


* * *' Find all the cells in the current selection.
* * For Each cell In Selection
* * * * *'This code repeats once for each cell in the selection.
* * * * cell.Value = Application.WorksheetFunction.Trim(cell.Value)
* * Next


End Sub- Hide quoted text -


- Show quoted text -


Try something like this:
Dim cell As Range
'Also Treat CHR 0160, as a space (CHR 032)
Selection.Replace What:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
'Trim in Excel removes extra internal spaces, VBA does not
On Error Resume Next 'in case no text cells in selection
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
cell.Value = Application.trim(cell.Value)
Next cell
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Add Trim Function to Code

Thanks Armando,

I will give that a try later today when I have a chance to get back to the
problem.



"Armando" wrote:

On Sep 29, 6:31 pm, Joyce wrote:
Hi there,

Thanks for your response.

I tested it as you recommended and the code is working. It seems to be the
actual cell content that doesn't trim. I tried it with the regular trim
function in Excel with the same result.

The data was imported, so perhaps there's something not apparent to the eye.

Back to the drawing board.



"Gary''s Student" wrote:
Your posted code works (removes leading and following and extra internal
spaces).


Make sure Selection is good. Just before the For loop put:


MsgBox(Selection.Address)
--
Gary''s Student - gsnu200905


"Joyce" wrote:


I have a sub that filters, selects cells and pastes them (transposed) as
headers in another area of the worksheet. I'd like to then trim the extra
spaces from the newly pasted headings. I'm very familiar with the Trim
function in Excel, but would like to to it in my sub. I've tried adding the
following code to my sub but, while it doesn't generate any error messages,
it just doesn't work:


-----Beginning of Code......
With Selection.(Formatting, etc.)
End With
Selection.SpecialCells(xlCellTypeConstants, 23).Select
------Then I add the following:


Dim cell


' Find all the cells in the current selection.
For Each cell In Selection
'This code repeats once for each cell in the selection.
cell.Value = Application.WorksheetFunction.Trim(cell.Value)
Next


End Sub- Hide quoted text -


- Show quoted text -


Try something like this:
Dim cell As Range
'Also Treat CHR 0160, as a space (CHR 032)
Selection.Replace What:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
'Trim in Excel removes extra internal spaces, VBA does not
On Error Resume Next 'in case no text cells in selection
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
cell.Value = Application.trim(cell.Value)
Next cell

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 with Country code leading Cassius Excel Worksheet Functions 3 June 9th 09 06:54 AM
Trim function exalan New Users to Excel 6 February 16th 09 04:21 AM
Code for Trim function shaji Excel Discussion (Misc queries) 2 June 15th 06 09:38 AM
one very, very bad line of code? Trim and chr(10) KR Excel Programming 2 March 22nd 05 09:35 PM
Trim function doesn't clean out ASCII Code 160 (Space) Ronald Dodge Excel Worksheet Functions 6 January 27th 05 03:48 AM


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