gpt4 book ai didi

excel - VBA 宏 : Validation not recognized

转载 作者:行者123 更新时间:2023-12-04 22:30:50 26 4
gpt4 key购买 nike

我正在尝试创建一个下拉列表,但 error 1004 'Application-defined or object-defined error'出现在 .add一直到Formula1 .

我努力了:

  • 删除 offset(0,3)
  • 一站式服务
  • 删除括号并将“.Add”移动到下一行
  • 复制我在网上找到的有效验证函数

  • 我不知道出了什么问题。我认为它可能不会被识别,但我不确定。
    Sheets("Sheet1").Range("C2").Offset(0, 3).Select
    MsgBox cell

    With Selection.Validation.Add(Type:=xlValidateList, _
    AlertStyle:=xlValidAlertStop, _
    Operator:=xlBetween, _
    Formula1:="B2:B5")
    .IgnoreBlank = True
    .InCellDropdown = True
    End With

    最佳答案

    不要使用 .Add在您的 With Block .

    Sheets("Sheet1").Range("C2").Offset(0, 3).Select

    With Selection.Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=$B$2:$B$5"
    .IgnoreBlank = True
    .InCellDropdown = True
    End With

    我建议避免 .Select并声明您的范围变量。
    Dim myRng As Range
    Set myRng = ThisWorkbook.Worksheets("Sheet1").Range("F2")

    With myRng.Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=$B$2:$B$5"
    .IgnoreBlank = True
    .InCellDropdown = True
    End With

    使用 .delete 确保您当前没有验证集可能不是一个坏主意。 - 如果您已经从之前的尝试中设置了数据验证集,它可能会引发错误。
    With myRng.Validation
    .delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=$B$2:$B$5"
    .IgnoreBlank = True
    .InCellDropdown = True
    End With

    关于excel - VBA 宏 : Validation not recognized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051002/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com