本文介绍了Excel宏“运行时错误”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“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!