简体   繁体   English

Excel宏“运行时错误'1004”

[英]Excel macro “Run-time error '1004”

I am new to scripting and I am trying to improve a existing Macro. 我是脚本新手,我正在尝试改进现有的宏。 I recorded a macro to remove dupliate and added it in a Main function which calls some other functions, but I am getting this error when I add the macro I recorded: Run-time error '1004': Application-defined or object-defined error. 我录制了一个宏来删除dupliate并将其添加到Main函数中,该函数调用其他函数,但是当我添加我记录的宏时出现此错误:运行时错误'1004':应用程序定义的或对象定义的错误。

The code looks like 代码看起来像

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

Thanks, Kiran 谢谢,Kiran

There is nothing wrong with your code. 您的代码没有任何问题。 You will only get this error if the Active worksheet is password protected. 如果Active工作表受密码保护,您将只会收到此错误。

Also it is a much better option to avoid using .Select and ActiveSheet . 另外,避免使用.SelectActiveSheet是一个更好的选择。 Your code can be written as 您的代码可以写成

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

FOLLOWUP 跟进

Sub DuplicateTest()
    ActiveSheet.Columns(1).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

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. 如果Microsoft Visual Basic应用程序在Excel 2003工作簿中复制并粘贴整行,则会发生此错误,或者如果Microsoft在此情况下复制并粘贴Excel 2003工作簿中的2,516行或更多行的范围,则可能会发生此错误运行时错误 1004发生。 To get solution of this error save the workbook and manipulate the code of the macro in which you can save workbook. 要获得此错误的解决方案,请保存工作簿并操作可以保存工作簿的宏代码。

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

Here is info I found about your situation, I hope it is helpful. 这是我发现你的情况的信息,我希望它是有帮助的。

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

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