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

时间:2017-05-18 01:32:50

标签: excel vba excel-vba

我目前正面临问题而且我不知道为什么......

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

它应该如何运作:

当我粘贴一个装满数据的行时,它应该检查是否为“是”或“否”的标准,然后将其分类到他们的独特工作表中。

BUT

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

还有一个问题:

在没有输入验证栏的情况下,有没有打印整件事?因为我试图首先键入验证列,当我继续填写它旁边的行时,它不显示在下一页上,只显示"是"或"否"哪一个是B栏

感谢任何帮助,感谢您的时间!

3 个答案:

答案 0 :(得分:4)

如果您更新整行,Target将是一整行。您无法测试整行是否为“是” - 您需要查看单个单元格。

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

关于是否有一种方法只有在整行完成之后才有这个过程的另一个问题,我建议你有一个按钮让用户在完成时点击按钮 - 这样就避免了Worksheet_Change事件。或者至少使最后一列成为触发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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

首先我建议将交集传递给变量

If MyTarget Is Nothing Then Exit Sub

接下来检查是否有交叉点,

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

现在可以使用

循环遍历该变量
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0

-Romcel