宏清除公式的问题,而不是清除Excel中的文本

时间:2016-03-24 13:11:02

标签: excel vba

以下宏设置为运行除主页之外的每个工作表。运行后,我在C列中的公式消失了,清理部分从未运行,因此每行都有访问文本。

    Sub testV2()
    Dim ws As Worksheet

     ActiveWorkbook.RefreshAll

    For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Macro" Then

    Columns("A:A").Select

    Selection.Replace What:="*identified", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Selection.Replace What:="as*", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False


ActiveSheet.Range("$A$1:$A$10000").RemoveDuplicates Columns:=1, Header:=xlNo

         Columns("A:A").Select
Columns("A:A").EntireColumn.AutoFit

With ws.UsedRange
  .Value = .Value
End With


    End If
    Next ws
    End Sub

1 个答案:

答案 0 :(得分:0)

我认为问题在于您没有激活当前使用的工作表以从中删除重复项。这样,ActiveSheet始终引用您开始运行代码时选择的工作表。它是否适用于以下代码?

Sub testV2()
    Dim ws As Worksheet

     ActiveWorkbook.RefreshAll

    For Each ws In ThisWorkbook.Worksheets
         If ws.Name <> "Macro" Then

        Columns("A:A").Select

        Selection.Replace What:="*identified", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        Selection.Replace What:="as*", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


        ws.Activate
        ActiveSheet.Range("$A$1:$A$10000").RemoveDuplicates Columns:=1, Header:=xlNo

        Columns("A:A").Select
        Columns("A:A").EntireColumn.AutoFit

         End If
    Next ws
End Sub