Vba 撤消Ctrl+;时出现类型不匹配的运行时错误13;多删除单元格的z

Vba 撤消Ctrl+;时出现类型不匹配的运行时错误13;多删除单元格的z,vba,excel,Vba,Excel,我正在学习VBA,尝试在我的live Excel工作表中使用 我有VBA代码,第一个得到 运行时错误13类型不匹配 在删除多个单元格时,由于工作量较小,我在删除多个单元格时不会出现错误,但在撤消多个删除的单元格时,会出现相同的错误 Private Sub Worksheet_Change(ByVal Target As Excel.Range) If Target.Column = 6 Then ThisRow = Target.Row If Target

我正在学习VBA,尝试在我的live Excel工作表中使用

我有VBA代码,第一个得到

运行时错误13类型不匹配

在删除多个单元格时,由于工作量较小,我在删除多个单元格时不会出现错误,但在撤消多个删除的单元格时,会出现相同的错误

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Column = 6 Then
        ThisRow = Target.Row
        If Target.Cells(1).Value = vbNullString Then Exit Sub
        If Len(Target.Offset(, -4)) = 0 Then
            Range("J" & ThisRow).Value = Time()
            Range("B" & ThisRow).Value = Date
            Range("AA" & ThisRow).Value = Environ("username")
        End If
    End If
End Sub

如果只对生成单个单元格更改的结果感兴趣(即目标是单个单元格),则可以添加:

If Target.Count > 1 Then Exit Sub 
多单元格更改将不会得到响应,并且您将不会得到多个单元格的类型不匹配错误
.Value
,因为此属性属于单个单元格范围对象


这也意味着只有粘贴单个单元格时才会响应。而且您不能一次更改多个单元格并获得预期结果

第二张图纸(Sheet2)用作填充单元格的存储。然后进行检查,以查看是否应响应当前事件1

Option Explicit
Public monitorCell As Range

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim ThisRow
    If Target.Column = 6 Then 
        ThisRow = Target.Row

        If Not IsEmpty(monitorCell) Or Target.Count > 1 Then Exit Sub

        If Len(Target.Offset(, -4)) = 0 Then
            Range("J" & ThisRow) = Time()
            Range("B" & ThisRow) = Date
            Range("AA" & ThisRow) = Environ$("username")
        End If
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Set monitorCell = Worksheets("Sheet2").Range(Target.Address)
End Sub
大概是这样的:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rng As Range, c As Range

    Set rng = Application.Intersect(Target, Me.Columns(6))
    If rng Is Nothing Then Exit Sub
    For Each c In rng.Cells
        If Len(c.Value) > 0 Then
            If Len(c.Offset(0, -4).Value) = 0 Then
                With c.EntireRow
                    .Cells(1, "J").Value = Time()
                    .Cells(1, "B").Value = Date
                    .Cells(1, "AA").Value = Environ("username")
                End With
            End If
        End If
    Next c
End Sub

这是否意味着一次只能为一个单元工作?你可以总是有一个If Target.Count>1,否则退出Sub。是的,大师,它的工作感谢assist这也意味着它只会在你粘贴单个单元格时响应。而且您不能一次更改多个单元格并获得预期结果。这就是你想要的吗?是的,你是对的,如果我再次修改,那么它会给出我不想要的修改后的时间和日期。请解释更多关于所需结果的信息?更改的时间和日期是什么意思?上面的代码我已经复制并粘贴到了新的工作表中,它给出了错误“运行时错误1004”应用程序定义或对象定义的错误调试在“如果Len(c.Offset(0,-4).Value)=0 Then”,当我将我的.columns(6)更改为我的.columns(2),它给出了错误“运行时错误1004”在“If Len(c.Offset(0,-4).Value)=0 Then”处调试应用程序定义或对象定义的错误,请帮助您不能从第2列向左偏移4列