下面的代码检查:
第二个代码块出现以下错误
错误-运行时错误13类型不匹配
在-If Not Target.Value Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
行上
如果有人可以帮助解决此错误,将不胜感激
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, r As Range, msg As String, x As Range
Set rng = Intersect(Columns(1), Target)
If Not rng Is Nothing Then
Application.EnableEvents = False
For Each r In rng
If Not IsEmpty(r.Value) Then
If Application.CountIf(Columns(1), r.Value) > 1 Then
msg = msg & vbLf & r.Address(0, 0) & vbTab & r.Value
If x Is Nothing Then
r.activate
Set x = r
Else
Set x = Union(x, r)
End If
End If
End If
Next
If Len(msg) Then
MsgBox "Duplicate values not allowed Invalid Entry" & msg
x.ClearContents
x.Select
End If
Set rng = Nothing
Set x = Nothing
Application.EnableEvents = True
End If
If Target.Column <> 1 Then Exit Sub
'check if format is valid BATCH00_00
If Not Target.Value Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
MsgBox "Invalid format!"
GoTo ValidationError
Exit Sub
End If
Exit Sub
ValidationError:
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
End Sub
电子表格中的示例数据
是的,我的电子表格中确实有数据
Batch_Code | Batch_Name | Batch_Invoker_Name
BATCH00_01 | DwhEtl_MetaData_Loading start | Invalid
BATCH00_02 | DwhEtl_MetaData_Loading | dwhetl_batch_meta_load.unx
BATCH00_03 | DwhEtl_MetaData_Loading1111 | dwhetl_batch_meta_load.unx
BATCH00_04 | DwhEtl_Reg_Files_R22213123 | dwhetl_batch_meta_load.unx
BATCH00_05 | DwhEtl_Reg_Files_R323131312 | dwhetl_batch_meta_load.unx
BATCH00_06 | DwhEtl_Reg_Files_R323131313 | dwhetl_batch_meta_load.unx
BATCH00_07 | DwhEtl_Reg_Files_R323131314 | dwhetl_batch_meta_load.unx
BATCH00_08 | DwhEtl_Reg_Files_R323131315 | dwhetl_batch_meta_load.unx
BATCH00_09 | DwhEtl_Reg_Files_R323131316 |
with Batch_Code as column 1 to be validated & checked with
-我也尝试了以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim validationError Boolean
validationError = False
'if changed cell was not in A column, then exit sub
If Target.Column <> 1 Then Exit Sub
'check if format is valid BATCH00_00
If Not Target.Value Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
MsgBox "Invalid format!"
validationError = True
Exit Sub
End If
'check for uniqueness in A column
If Application.WorksheetFunction.CountIf(Range("A:A"), Target.Value) > 1 Then
MsgBox "Values must be unique in A column!"
validationError = True
End If
If validationError Then
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
End If
End Sub
还有一个:
If Target.Column <> 1 Then Exit Sub
If IsError(Target.Column) Then
'check if format is valid BATCH00_00
If Not Target.Column Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
MsgBox "Invalid format!"
GoTo ValidationError
Exit Sub
End If
End If
Exit Sub
ValidationError:
Application.EnableEvents = False
Target.value = ""
Application.EnableEvents = True
End Sub
答案 0 :(得分:1)
错误来了,因为Target.Value
是某种错误。因此,错误不能与任何事物进行比较,并且会引发此类型不匹配错误。尝试这段代码来复制:
Public Sub TestMe()
Range("A1") = "=6 / 0" 'Making a #DIV/0! to replicate
If Range("A1") Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
Debug.Print "SOMETHING"
End If
End Sub
因此,在您的代码中,最简单的方法就是以某种方式清理输入。例如,检查是否不是错误。使用IsError()
函数很容易做到这一点:
Public Sub TestMe()
Range("A1") = "=6 / 0"
If Not IsError(Range("A1")) Then
If Range("A1") Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
Debug.Print "SOMETHING"
End If
End If
End Sub
因此,在您的原始代码中,尝试以下操作:
If Not IsError(Target) Then
If Not Target.Value Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
MsgBox "Invalid format!"
GoTo ValidationError
Exit Sub
End If
End If
答案 1 :(得分:1)
我上面的答案应该标记为正确。
使用直接从OP复制的代码和输入并运行会导致预期的行为。重复的条目和无效格式的返回预期消息。进一步的测试表明,OP的问题与上述问题完全相同。通过在列A中使用公式“ = 5/0”创建错误来验证这一点。答案中正确诊断为返回的错误是运行时错误,请键入不匹配。
OP显然没有调查建议给他的途径。
这是我用来测试的代码,唯一的变化是将选项显式添加,将debug.print输出添加到测试分支,添加1条注释行,并在一行中添加1条声明。除了空格,没有其他更改。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim r As Range
Dim msg As String
Dim x As Range
Set rng = Intersect(Columns(1), Target)
If Not rng Is Nothing Then
Application.EnableEvents = False
For Each r In rng
If Not IsEmpty(r.Value) Then
If Application.CountIf(Columns(1), r.Value) > 1 Then
msg = msg & vbLf & r.Address(0, 0) & vbTab & r.Value
If x Is Nothing Then
r.Activate
Set x = r
Else
Set x = Union(x, r)
End If
End If
End If
Next
If Len(msg) Then
MsgBox "Duplicate values not allowed Invalid Entry" & msg
x.ClearContents
x.Select
End If
Set rng = Nothing
Set x = Nothing
Application.EnableEvents = True
End If
If Target.Column <> 1 Then Exit Sub
'check if format is valid BATCH00_00
If Not Target.Value Like "BATCH[0-9][0-9]_[0-9][0-9]" Then
MsgBox "Invalid format!"
GoTo ValidationError
Debug.Print "should never be run"
Exit Sub 'what's the point of this ?
End If
Debug.Print "normal exit"
Exit Sub
ValidationError:
Application.EnableEvents = False
Target.Value = ""
Application.EnableEvents = True
Debug.Print "exit from validation error"
End Sub
这是示例输入。消除Div / 0错误,程序运行。
Batch_Code Batch_Name Batch_Invoker_Name
BATCH00_01 DwhEtl_MetaData_Loading start Invalid
BATCH00_02 DwhEtl_MetaData_Loading dwhetl_batch_meta_load.unx
BATCH00_03 DwhEtl_MetaData_Loading1111 dwhetl_batch_meta_load.unx
BATCH00_04 DwhEtl_Reg_Files_R22213123 dwhetl_batch_meta_load.unx
BATCH00_05 DwhEtl_Reg_Files_R323131312 dwhetl_batch_meta_load.unx
BATCH00_06 DwhEtl_Reg_Files_R323131313 dwhetl_batch_meta_load.unx
BATCH00_07 DwhEtl_Reg_Files_R323131314 dwhetl_batch_meta_load.unx
BATCH00_08 DwhEtl_Reg_Files_R323131315 dwhetl_batch_meta_load.unx
BATCH00_09 DwhEtl_Reg_Files_R323131316
BATCH10_10
#DIV/0!