使用列表验证单元格:应用程序定义或对象定义错误

时间:2014-12-19 13:36:28

标签: excel vba excel-vba

我使用下面的代码将验证列表添加到各个单元格。我认为这很简单,但我在Formula1:="Notes!A1" & finalRowNotes行收到错误。错误是

  

应用程序定义或对象定义错误

我错过了什么?

finalRowNotes = Worksheets("Notes").Cells(1000000, 1).End(xlUp).Row
For i = 4 To r - 1
    With Range("P" & i).Validation
      .Delete
      .Add Type:=xlValidateList, _
      AlertStyle:=xlValidAlertStop, _
      Operator:=xlBetween, _
      Formula1:="=Notes!A1:A" & finalRowNotes
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
    End With
  Next i

对于它的价值,这里是单元格A1:A18中的Notes工作表中的内容

Block volume not reported
Blocks
Blocks away
Blocks.  Foreign trades not incl. in volume or VWAP in this market
Could not inventory
Market closed
No foreign trades
Order canceled
Order complete
Order given after market
Order given mid-session
Out of limit
Out of limit, changed to market order after session closed
Out of limit-premium
Pending 1 day funding requirement
Pending custodian confirmation
Volume out of limit
Volume out of limit, limit reduced during session

4 个答案:

答案 0 :(得分:0)

根据问题和第一个答案中的对话,我建议将以下代码放在一个模块中:

'Dimension your two variables
Dim Notes As Worksheet, Vali As Worksheet

'Set the variables equal to the sheet names.
Set Notes = Sheets("Notes")
Set Vali = Sheets("NAME OF VALIDATION SHEET")

'Changes the Sheet reference to the variable Notes and changed your syntax to Rows.Count
finalRowNotes = Notes.Cells(Rows.Count, 1).End(xlUp).Row 'Rows.Count is better method for last row of document
For i = 4 To r - 1
    'Added Vali to define the sheet
    With Vali.Range("P" & i).Validation
      .Delete
      .Add Type:=xlValidateList, _
      AlertStyle:=xlValidAlertStop, _
      Operator:=xlBetween, _
      Formula1:="=Notes!A1:A" & finalRowNotes
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
    End With
  Next i

答案 1 :(得分:0)

我不知道该说些什么,而且我很遗憾浪费每个人的时间,但是当我将这个工作簿恢复到早期版本时,一切都很好。我所能想到的只是感谢所有在帽子上戴帽子并做出如此慷慨努力的人。我想我的工作簿在某种程度上已经被破坏了,因为一个较旧但相同的工作簿按预期工作。

答案 2 :(得分:0)

您使用operator xlBetween。我想,你需要xlEqual。

答案 3 :(得分:0)

只需激活范围,就可以解决问题..

Vali.Range("P" & i).Activate