【问题标题】:VBA Macro: Application-defined or object-defined errorVBA 宏:应用程序定义或对象定义的错误
【发布时间】:2017-05-18 02:54:27
【问题描述】:

我正在为 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

请帮助我了解我收到此错误的原因以及如何解决它?

【问题讨论】:

  • 选择在活动表中,更改为您的 with 语句中的完整引用范围。视情况而定,您首先需要在添加之前删除验证

标签: vba excel


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多