2017-08-07 65 views
0

我正在尝试在受保护的文档中添加下拉列表。添加数据验证时的运行时错误1004

我使用的代码是:

Private Sub AddDropDown_Click() 
    ActiveSheet.Unprotect 

    Dim currCell As Range 
    Dim lastRow As Long 
    With ActiveSheet 
     lastRow = ActiveSheet.Cells(1048576, 1).End(xlUp).Row 
    End With 
    For Each currCell In Range("K4:K" & lastRow) 
     With currCell.Validation 
      .Delete 
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=INDIREKT(J" & currCell.Row & ")" 
      .IgnoreBlank = True 
      .InCellDropdown = True 
      .InputTitle = "" 
      .ErrorTitle = "" 
      .InputMessage = "" 
      .ErrorMessage = "" 
      .ShowInput = True 
      .ShowError = True 
     End With 
    Next currCell 
    ActiveSheet.Protect 
End Sub 

但是当我尝试运行它抛出Error 1004。新增的excecuted该行的代码。

大多数参数抛出当我尝试whatching他们在调试时相同的错误:

Debug Screenshot Validation

有没有人有一个想法是什么原因造成的?

+1

首先,你需要用范围限定你的'formula',所以它需要是'cell.formula <>“”''。第二,什么是INDIREKt?你在使用Excel的德语设置吗? –

+0

你的'公式'变量集在哪里? ('如果公式<>“”Then')你是不是指'Cell.Formula'?其次,您在代码中使用德语版的“间接”公式 - 试试英语。 –

+0

@ShaiRado是的,我有德语的Excel版本,你也可以在屏幕截图中看到。在改变一些事情之前,公式是我在那里的一部分。从问题中删除它。 – lightlike

回答

0

德国配方是好的。但尝试以下这些要点进行调试:

  1. 上的错误恢复删除下一
  2. 重命名cell其他任何事情。例如。 myCellrngCell
  3. debug.print lastRow放在For Each cell In Range("K4:K" & lastRow)之前。
  4. Option Explicit放在顶部并尝试编译代码。
+0

做了这一切,并改变了它的问题。仍然不起作用。 “On Error”也不起作用。我仍然收到错误消息。 “lastRow”是2214,这是正确的。 – lightlike