excel数据验证错误

时间:2015-03-15 23:24:41

标签: excel vba validation

我正在尝试编写数据验证列表,当它添加一个新行时,我如何不断获取运行时错误'1004'应用程序定义或对象定义错误。

一直在这里搜索并尝试不同的代码,我可以得到我的手,但似乎所有似乎给我同样的错误。

我确实设法让它先前工作但不确定为什么它现在不起作用。

以下是除数据验证部分之外的子代码。

Sub AddNewRecord()
Dim vNewRow As Long

' Find the first empty row in the data table
vNewRow = DataTable.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

' Check for data in Field 1
If Trim(Range("formField1").Value) = "" Then
    Range("formField1").Activate
    MsgBox "Please enter data in Field 1!"
    Exit Sub
End If
' Check for data in Field 2
If Trim(Range("formField2").Value) = "" Then
    Range("formField2").Activate
    MsgBox "Please enter data in Field 2!"
    Exit Sub
End If

' Copy the data to the data table
With DataTable
    .Cells(vNewRow, 1).Value = Range("formField1").Value
    .Cells(vNewRow, 2).Value = Range("formField2").Value
End With

' Add data validation to data table
With DataTable
    .Activate
    .Cells(vNewRow, 3).Select

    With Range("G" & vNewRow).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
             xlBetween, Formula1:="In Progress,Completed"
    End With

'        With Selection.Validation
'            .Delete
'            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
'            xlBetween, Formula1:="In Progress,Completed"
'            .IgnoreBlank = True
'            .InCellDropdown = True
'            .InputTitle = ""
'            .ErrorTitle = ""
'            .InputMessage = ""
'            .ErrorMessage = ""
'            .ShowInput = True
'            .ShowError = True
'        End With
End With

' Clear data fields and reset the form
With InputForm
    .Activate
    .Range("formField1").Value = ""
    .Range("formField2").Value = ""
    .Range("formField1").Select
    .Visible = False
End With
With DataTable
    .Activate
    .Cells(vNewRow, 1).Select
End With
End Sub

感谢。

0 个答案:

没有答案