Remove Leading and Trailing Whitespace from Excel Cells

余生颓废 提交于 2019-12-24 06:34:04

问题


I have an Excel spreadsheet with several cells that have leading or trailing whitespace (including spaces, non-breaking spaces, carriage returns, and new lines). I want to remove these white spaces. My Google searching suggests this can only be done with VBA. The VBA Trim function doesn't remove all types of whitespaces and may remove from the middle of the cell as well. The MSDN page for VBA's Trim function references .NET's String.Trim method which does exactly what I want. However, I'm not sure how to call String.Trim from VBA as suggested by the first link. How do I call the String.Trim method from Excel VBA?

EDIT: To clarify, I want to keep the whitespace (including carriage returns and new lines) in the middle of the cell. I only want to remove the characters from the beginning and the end of the cell.


回答1:


If you want to do this in Excel without having to go through VBA use:

=TRIM(CLEAN(A1))

Assuming you want to clean cell A1. From my tests this removes all tabs, carriage return and line feeds, but also converts multiple spaces to single ones so it may not be exactly what you want.




回答2:


I was having the same issue but i found this code and its working perfect. It removes leading and trailing space from column name .

foreach (DataColumn c in d.Columns)
                c.ColumnName = c.ColumnName.Trim();

Cheers



来源:https://stackoverflow.com/questions/37793920/remove-leading-and-trailing-whitespace-from-excel-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!