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

我有一个VBAmacros从一个主工作簿复制两张表(一个表单来收集数据,一张表和一批查找下拉菜单),填充新的工作簿并保存。

由于下拉式数据validation列表在工作表复制时更新其链接,因此我必须重置validation以引用新的“ 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(“应用程序定义的或对象定义的错误”)。

如果有任何方法可以复制工作表,而不自动更新数据validation ,那么如果没有,那么是否有人知道是什么原因导致代码崩溃以及如何修复?

在尝试创build一个新的数据之前,先删除任何以前的数据validation。

 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