如果我使用Trim with Replace函数作为MyCell.Value = Trim(Replace(MyCell.Value, Chr(160), Chr(32)))
,它将删除我的前导零。
Sub TrimText()
Dim MyCell As Range
On Error Resume Next
Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select
For Each MyCell In Selection.Cells
MyCell.Value = TRIM(SUBSTITUTE(G7, CHAR(160), " "))
Next
On Error GoTo 0
End Sub
答案 0 :(得分:0)
Sub TrimText()
Dim MyCell As Range
On Error Resume Next
Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select
For Each MyCell In Selection.Cells
'Range("K:K").Select
Selection.NumberFormat = "@"
MyCell.Value = Trim(Replace(MyCell.Value, Chr(160), Chr(32)))
Next
On Error GoTo 0
End Sub
答案 1 :(得分:-1)
您的问题不是Substitute
功能,而是CHAR
。请改用Chr
。
<强>更新强>
对不起,替补也错了。写WorksheetFunction.Substitute(...)
。
SUBSTITUTE
和CHAR
仅用作Excel函数,但不像你使用它们那样用作VBA函数。