Excel macro “Run-time error '1004”

后端 未结 3 1197
闹比i
闹比i 2020-12-22 01:09

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, b

相关标签:
3条回答
  • 2020-12-22 01:24

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

    Here is info I found about your situation, I hope it is helpful.

    0 讨论(0)
  • 2020-12-22 01:28

    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.

    0 讨论(0)
  • 2020-12-22 01:45

    There is nothing wrong with your code. You will only get this error if the Active worksheet is password protected.

    Also it is a much better option to avoid using .Select and ActiveSheet. 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
    
    0 讨论(0)
提交回复
热议问题