问题
I have an Excel file that contains some values. I need to check if a cell value contains spaces. For example, sometimes a user will write some values in the cell but mistakenly press Enter or Space in the cell. I want to see if the cell value has spaces inside... Please tell me how to do this.
Thanks
回答1:
Let's say I type "ABC " in cell A1 (ABC followed by a space). Either of these formulas should help:
=LEN(A1)=LEN(TRIM(A1))
=EXACT(A1,TRIM(A1))
For a VBA version:
Function HasSpaces(rng As Excel.Range) As Boolean
HasSpaces = Not (Len(rng.Value) = Len(Trim$(rng.Value)))
End Function
Sample usage:
Sub tst()
Debug.Print HasSpaces(Range("A1"))
End Sub
来源:https://stackoverflow.com/questions/8212637/how-to-check-that-excel-cell-value-has-spaces-excel-2007