Trim Macro

danjw_98

Active Member
Joined
Oct 25, 2003
Messages
354
I would like to trim data (removing extra spaces) in column b and keep the trimmed data in the same column b - is there possibly a macro that can do this.

thanks...
 
md_iffu,

Here is another macro solution for you to consider based on your single example, and, result.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub TrimColumnR_V2()
' hiker95, 05/20/2015, ME83611
With ActiveSheet.Range("R1:R" & Cells(Rows.Count, "R").End(xlUp).Row)
  .Value = Evaluate("IF(ROW(),SUBSTITUTE(" & .Address & ","" ."","".""),"""")")
End With

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the TrimColumnR_V2 macro.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,

Am using a PC.
Below is the example.
in the below column as you can see "location" details.
I want this column to be trimmed and get pasted as values in the same column.

Location
Apex, NC
West Conshohocken, PA
Franklin, MA
Seattle, WA
Toronto, On, Canada


<tbody>
</tbody><colgroup><col></colgroup>
 
Upvote 0
Here is another macro solution for you to consider based on your single example, and, result.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub TrimColumnR_V2()
' hiker95, 05/20/2015, ME83611
With ActiveSheet.Range("R1:R" & Cells(Rows.Count, "R").End(xlUp).Row)
  .Value = Evaluate("IF(ROW(),SUBSTITUTE(" & .Address & ","" ."","".""),"""")")
End With
Although the example is not clear to me, Message #12 seems to indicate the OP wants something different than what your macro does; however, I just wanted to point out that there is a simpler (and probably quicker) code solution to remove the space from in front of a period...
Code:
Sub TrimColumnR_V2()
  Columns("R").Replace " .", ".", xlPart
End Sub
 
Upvote 0
Am using a PC.
Below is the example.
in the below column as you can see "location" details.
I want this column to be trimmed and get pasted as values in the same column.
What you existing data looks like is not clear because the comment processor on this forum removes multiple contiguous spaces and replaces them with single spaces. Post your existing example data again, but surround it with code tags (use the # button in the Reply windows Ribbon) as that will preserve multiple spaces. Then post your desired result for that data, again, using code tags.
 
Upvote 0
md_iffu,

I want this column to be trimmed and get pasted as values in the same column.

Here is another macro solution for you to consider based on your above quote.

Sample raw data:


Cell Formulas
RangeFormula
R2="Apex, NC ."
R3="West Conshohocken, PA ."
R4="Franklin, MA ."
R5="Seattle, WA ."
R6="Toronto, On, Canada ."


After the new macro:


Excel 2007
R
1Location
2Apex, NC.
3West Conshohocken, PA.
4Franklin, MA.
5Seattle, WA.
6Toronto, On, Canada.
7
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub TrimColumnR_V3()
' hiker95, 05/22/2015, ME83611
With ActiveSheet.Range("R2:R" & Cells(Rows.Count, "R").End(xlUp).Row)
  .Value = .Value
  .Value = Evaluate("IF(ROW(),SUBSTITUTE(" & .Address & ","" ."","".""),"""")")
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the TrimColumnR_V3 macro.
 
Upvote 0
md_iffu,

If my reply #15 is still not correct, then we will need to see your actual raw data workbook/worksheet.

You can upload your workbook to (the BLUE link-->) Box Net ,

sensitive data changed

mark the workbook for sharing

and provide us with a link to your workbook.
 
Upvote 0
Rich (BB code):
Sub TrimColumnR_V3()
' hiker95, 05/22/2015, ME83611
With ActiveSheet.Range("R2:R" & Cells(Rows.Count, "R").End(xlUp).Row)
  .Value = .Value
  .Value = Evaluate("IF(ROW(),SUBSTITUTE(" & .Address & ","" ."","".""),"""")")
End With
End Sub
You can delete the code line that I highlighted in red... the Evaluate function in the next line will automatically return values directly, there is no need to change the formulas to values beforehand for it.
 
Upvote 0
Private Message from md_iffu:

Thank you

Hi,

Appreciate your quick response on the macro provided #15 is working fine.

Going further if I need any help, will post or check the forum.

Regards,
Mohammed Irfan
md_iffu

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top