简体   繁体   English

使用vba删除excel中的chr160个字符

[英]Deleting chr160 characters in excel using vba

In the following code I would like to delete the char 160 and 34 characters at the end of the text string.在下面的代码中,我想删除文本字符串末尾的char 16034字符。

For Column = 12 To 13

    For i = 16 To last_row
        'sString = ("=CODE(160)")
        'Range(Cells(i, Column), Cells(i, Column)).Select
        'Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Value = Replace(Range(Cells(i, Column), Cells(i, Column)).Value, sString, "", 1, -1)

        Set current_cell = Range(Cells(i, Column), Cells(i, Column))
        current_cell.Select

        text_to_change = Range(Cells(i, Column), Cells(i, Column)).Value

        Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Select
        Selection.NumberFormat = "General"
        With Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column))
        .Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")"
        .Value = .Value
        End With

    Next i

Next Column

I'm getting an error at this line:我在这一行遇到错误:

.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")" . .Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")"

The error message indicates:错误消息指出:

Run-time error '1004': Application-defined or object-defined error.运行时错误“1004”:应用程序定义或对象定义的错误。

Could you help me what is wrong here?你能帮我这里有什么问题吗?

您需要转义引号:

.Formula = "=SUBSTITUTE(""" & text_to_change & """;CODE(160);""" & Chr(34) & Chr(34) & """)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM