VBA从CELL中清除字符

时间:2015-04-24 20:46:24

标签: excel vba replace

VBA从CELL清除字符,这个脚本是通过打开的工作簿中的所有工作表并删除特殊字符(所以我可以重命名使用CELL的TAB )但是它只是清理运行脚本的页面。

Sub CleanChar()
Dim ws As Worksheet
Dim myString As String
Dim newString As String

For Each ws In Worksheets
    With ws
        myString = Range("A1").Value
        newString = Replace(Replace(myString, ":", ""), """", "")
        Range("A1").Offset().Value = newString
    End With
Next ws

End Sub

1 个答案:

答案 0 :(得分:1)

我认为问题可能是您的.对象前面缺少Range。您说明With ws但实际上并未使用ws,因此您的Range对象仅指一个工作表。

试试这个稍微修改过的代码:

Sub CleanChar()

    Dim ws As Worksheet
    Dim myString As String
    Dim newString As String

    For Each ws In Worksheets
        With ws
            myString = .Range("A1").Value
            newString = Replace(Replace(myString, ":", ""), """", "")
            .Range("A1").Offset().Value = newString
        End With
    Next ws

End Sub