首页 文章

在Excel中通过VBA设置下拉列表验证

提问于
浏览
1

我有一个VBA宏来复制两个工作表(一个用于收集数据的表单,一个用于下拉菜单的一批查找),从主工作簿中填充新工作簿并将其保存 .

由于下拉数据验证列表在复制工作表时更新其链接,因此我必须重置验证以引用新的 Lookups 工作表 . 目前,我正在尝试这个(当然有不同的坐标):

With wsNew.Cells(19, 5)  ' Display on web schedule
    .Value = wsData.Cells(11, iColCount).Value
    .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Lookups!$B$2:$B$3"
    .Validation.IgnoreBlank = True
    .Validation.InCellDropdown = True
End With

一旦到达 .Validation.Add Type... 行,它就会出现1004("Application-defined or object-defined error")错误 .

If there's any way to copy the sheet without it automatically updating the data validation 这将是理想的,但如果没有't, does anyone know what'导致该代码中断以及如何解决它?

1 回答

  • 1

    在尝试创建新数据之前删除任何先前的数据验证 .

    With wsNew.Cells(19, 5)  ' Display on web schedule
        .Value = wsData.Cells(11, iColCount).Value
        .Validation.DELETE
        .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Lookups!$B$2:$B$3"
        .Validation.IgnoreBlank = True
        .Validation.InCellDropdown = True
    End With
    

相关问题