簡體   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