Excel宏“运行时错误”1004“ [英] Excel macro "Run-time error '1004"
问题描述
我记录了一个宏以删除dupliate并将其添加到调用其他函数的Main函数中,但是当我添加我记录的宏时,我收到此错误:
运行时错误'1004':
应用程序定义或对象定义的错误。
代码看起来像
调用DeleteBlankRows
调用TrimText
结束
Sub DeleteBlankRows()
。
。
End Sub
Sub TrimText()
。
。
End Sub
Sub DuplicateRemove()
列(A:A)。选择
ActiveSheet.Range($ A $ 1:$ A $ 95678) .RemoveDuplicates列:= 1,标题:= xlNo
End Sub
谢谢
Kiran
您的代码没有任何问题。如果 Active
工作表受密码保护,您将只会收到此错误。
此外,这是一个更好的选择避免使用。选择
和 ActiveSheet
。您的代码可以写成
Sub DuplicateRemove()
Dim ws As Worksheet
设置ws = Sheets(Sheet1)
如果.ProtectContents = True然后
MsgBoxWorksheet被保护
.UnprotectMYPASSWORD
.Range($ A $ 1:$ A $ 95678)RemoveDuplicates列:= 1,标题:= xlNo
.ProtectMYPASSWORD
Else
.Range($ A $ 1:$ A $ 95678)。RemoveDuplicates列:= 1,标题:= xlNo
如果
结束
结束Sub
FOLLOWUP
Sub DuplicateTest )
ActiveSheet.Columns(1).RemoveDuplicates列:= 1,标题:= xlNo
End Sub
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.
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
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
这篇关于Excel宏“运行时错误”1004“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!