粘贴整行数据时VBAtypes不匹配(运行时错误13)

我目前面临的问题,我不知道为什么..

Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Columns("B:B")) Is Nothing Then Exit Sub If Target.Value = "Yes" Then Range(Range("A" & Target.Row), Range("I" & Target.Row)).Copy _ Sheets("UpdateModify Forms").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) Range(Range("AC" & Target.Row), Range("AU" & Target.Row)).Copy _ Sheets("UpdateModify Forms").Range("J" & Rows.Count).End(xlUp).Offset(1, 0) ElseIf Target.Value = "No" Then Range(Range("A" & Target.Row), Range("AB" & Target.Row)).Copy _ Sheets("Development Forms").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) Range(Range("AW" & Target.Row), Range("AY" & Target.Row)).Copy _ Sheets("Development Forms").Range("AC" & Rows.Count).End(xlUp).Offset(1, 0) End If End Sub 

错误指向

 If Target.Value = "Yes" Then 

它应该如何工作:

当我粘贴一行填充数据,正确的时候应该检查是否是的标准,并将其sorting到他们独特的表。

它出现了运行时错误13出于某种原因。我只知道如何绕过它,这是手动input数据,并避免首先触发validation的列,首先键入其他数据,最后是validation列。

还有一个问题:

无论如何打印整个事情没有我inputvalidation列最后? 因为我试图首先键入validation列,当我继续填写旁边的行,它不显示在下一张表,只显示“是”或“否”,这是列B

任何帮助将不胜感激,谢谢你的时间!

如果你更新整行, Target将会是整行。 您无法testing整行是否为“是” – 您需要查看单个单元格。

 Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Columns("B")) Is Nothing Then Exit Sub Dim cel As Range For Each cel In Intersect(Target, Columns("B:B")).Cells If cel.Value = "Yes" Then Range(Range("A" & cel.Row), Range("I" & cel.Row)).Copy _ Sheets("UpdateModify Forms").Range("A" & Sheets("UpdateModify Forms").Rows.Count).End(xlUp).Offset(1, 0) Range(Range("AC" & cel.Row), Range("AU" & cel.Row)).Copy _ Sheets("UpdateModify Forms").Range("J" & Sheets("UpdateModify Forms").Rows.Count).End(xlUp).Offset(1, 0) ElseIf cel.Value = "No" Then Range(Range("A" & cel.Row), Range("AB" & cel.Row)).Copy _ Sheets("Development Forms").Range("A" & Sheets("Development Forms").Rows.Count).End(xlUp).Offset(1, 0) Range(Range("AW" & cel.Row), Range("AY" & cel.Row)).Copy _ Sheets("Development Forms").Range("AC" & Sheets("Development Forms").Rows.Count).End(xlUp).Offset(1, 0) End If Next End Sub 

至于另一个问题是否只有在整行被​​填充之后才有这个过程,我build议你有一个button,让用户在完成时单击button – 这样就避免了一个Worksheet_Change事件。 或者至less让最后一列成为引发Change那一列。


针对评论请求值:

 Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Columns("B:B")) Is Nothing Then Exit Sub Dim cel As Range For Each cel In Intersect(Target, Columns("B:B")).Cells If cel.Value = "Yes" Then With Sheets("UpdateModify Forms") With .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).EntireRow .Range("A1:I1").Value = Rows(cel.Row).Range("A1:I1").Value 'I commented out the next two lines because I assume the 'last row in column A should be the same as the last row 'in column J - uncomment them if that is not the case. 'End With 'With .Range("J" & .Rows.Count).End(xlUp).Offset(1, 0).EntireRow .Range("J1:AB1").Value = Rows(cel.Row).Range("AC1:AU1").Value End With End With ElseIf cel.Value = "No" Then With Sheets("Development Forms") With .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).EntireRow .Range("A1:AB1").Value = Rows(cel.Row).Range("A1:AB1").Value 'End With 'With .Range("AC" & .Rows.Count).End(xlUp).Offset(1, 0).EntireRow .Range("AC1:AE1").Value = Rows(cel.Row).Range("AW1:AY1").Value End With End With End If Next End Sub 

当select多个单元格时,尝试使用target.value时会出错。 当selection.cells.count > 1时尝试使用target.values

首先我build议通过交叉点variables,

 Dim MyTarget as range Set MyTarget = Intersect(Target, Columns("B:B")) 

接下来检查是否有路口,

 If MyTarget Is Nothing Then Exit Sub 

您现在可以使用循环通过该variables

 Dim EachCell as Range For Each EachCell in MyTarget 'do your thing here Next 

-Romcel