我想从excel中删除尾随零。但是,当我尝试使用替代功能时,我得到函数未定义错误

时间:2014-12-02 07:05:14

标签: excel excel-vba vba

如果我使用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

2 个答案:

答案 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(...)

SUBSTITUTECHAR仅用作Excel函数,但不像你使用它们那样用作VBA函数。