繁体   English   中英

VBA宏:应用程序定义或对象定义的错误

[英]VBA Macro: Application-defined or object-defined error

我在VBA宏上的语法上苦苦挣扎。 试图从工作表1的工作表2列表中创建一个选择,它给了我运行时错误'1004':应用程序定义的错误或对象定义的错误。

Sub Macro1()

Sheets("Sheet1").Select
Sheets("Sheet1").Range("G3").Select
With Selection.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="Sheet2!$A$1:$A$14"
.IgnoreBlank = True
.InCellDropdown = True
End With


End Sub

请帮助我了解为什么会收到此错误以及如何解决该错误?

如果您多次运行宏,则需要确保删除所有现有的验证

Sub Macro1()
    With Sheets("Sheet1").Range("G3").Validation
        'Remove existing validation
        .Delete
        'Add new validation
        .Add Type:=xlValidateList, _
             AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, _
             Formula1:="=Sheet2!$A$1:$A$14"
           'Note - need ^ (i.e. the equals sign to make it a formula)
        .IgnoreBlank = True
        .InCellDropdown = True
    End With
End Sub

暂无
暂无

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

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