1

我是脚本新手,我正在尝试改进现有的宏。我录制了一个宏以删除重复项并将其添加到调用其他一些函数的 Main 函数中,但是当我添加我录制的宏时出现此错误:运行时错误“1004”:应用程序定义或对象定义错误.

代码看起来像

Sub Main()
Call DuplicateRemove
Call DeleteBlankRows
Call TrimText
End 

Sub DeleteBlankRows()
.
.
End Sub

Sub TrimText()
.
.
End Sub

Sub DuplicateRemove()
Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$95678").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

谢谢,基兰

4

3 回答 3

3

您的代码没有任何问题。Active如果工作表受密码保护,您只会收到此错误。

.Select避免使用and也是一个更好的选择ActiveSheet。你的代码可以写成

Sub DuplicateRemove()
    Dim ws As Worksheet

    Set ws = Sheets("Sheet1")

    With ws
        If .ProtectContents = True Then
            MsgBox "Worksheet is protected"
            .Unprotect "MYPASSWORD"
            .Range("$A$1:$A$95678").RemoveDuplicates Columns:=1, Header:=xlNo
            .Protect "MYPASSWORD"
        Else
            .Range("$A$1:$A$95678").RemoveDuplicates Columns:=1, Header:=xlNo
        End If
    End With
End Sub

跟进

Sub DuplicateTest()
    ActiveSheet.Columns(1).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
于 2012-07-31T21:41:12.040 回答
1

This error occur if The Microsoft Visual Basic Applications copies and pastes whole row in an Excel 2003 workbook or this error may be occur if the Microsoft copies and pastes a range of 2,516 rows or more rows in an Excel 2003 workbook at this cases runtime error 1004 occurs. To get solution of this error save the workbook and manipulate the code of the macro in which you can save workbook.

于 2014-02-27T12:33:39.257 回答
-1

ActiveSheet.Range("A1:C100").RemoveDuplicates Columns:=Array(1,2), Header:=xlYes

这是我找到的关于您的情况的信息,希望对您有所帮助。

于 2012-07-31T18:30:08.007 回答