我有一些最初由here修改的代码。当行的第一列中有一个C时,该行将被删除并保存在另一个工作表中。这是一个待办事项列表应用程序。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Code goes in the Worksheet specific module
Dim rng As Range
' Set Target Range
Set rng = Target.Parent.Range("A1:A200")
' Only look at single cell changes
If Target.Count > 1 Then Exit Sub
' Only look at that range
If Intersect(Target, rng) Is Nothing Then Exit Sub
' Action if Condition(s) are met
Select Case Target.Text
Case "C"
Target.EntireRow.Copy Sheets("Completed").Cells(Rows.Count, "A").End(xlUp).Offset(1)
Target.EntireRow.Delete
End Select
End Sub
代码在Excel 2010上运行得非常好,但失败并出现此错误:
Run time error '1004' "Copy Method of Range Class Failed"
答案 0 :(得分:1)
这是你在尝试什么?您需要关闭其他事件Target.EntireRow.Delete
才会重新启动该事件。您可能还想查看This
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lRow As Long
'~~> For Excel 2007+
'If Target.Cells.CountLarge > 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
On Error GoTo Whoa
Application.EnableEvents = False
If Not Intersect(Target, Range("A1:A200")) Is Nothing Then
If Target.Value = "C" Then
With Sheets("Completed")
'~~> Find next available row in the output sheet
lRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
Target.EntireRow.Copy .Rows(lRow)
Target.EntireRow.Delete
End With
End If
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
答案 1 :(得分:0)
Sheets("Completed").Cells(Rows.Count, "A").End(xlUp).Offset(1)
需要
Sheets("Completed").Cells(Sheets("Completed").Rows.Count, "A").End(xlUp).Offset(1)