Sheet1
被称为Action Items
,Sheet2
被称为Completed Items
。
J
中的Completed Items
列包含在下拉列表中排序的值。只要将值设置为O
,P
或D
,就应按以下方式将此行重新定位到Action Items
:
A
包含两个表格中从行5
到行1005
的ID号。1
位于5
行,1
列Completed Items
,标识2
位于5
行,列1
的{{1}}。Action Items
中的J5
设置为Completed Items
,O
或P
,我希望VBA代码在{{中插入新行1}}并按以下方式从D
填充该行与相应行的内容:
Action Items
行Completed Items
重新定位到行Action Items
,将行5
重新定位到行6
,等等。也就是说,在那里插入一个空行(唯一)ID 小于,而不是下面行中的ID,因此在给定的示例中,它将是行6
中的ID 7
和ID 1
然后在5
行等等2
删除修改后的行。即:所有代码正在进行的是将一行从一个工作表重新定位到另一个工作表。但是,我当前的代码仍会导致一些问题(6
= Completed Items
和Zeile2
= Row2
):
Zeile1
此代码不能
我尝试更改某些数字,甚至尝试使用Row1
选项处理Private Sub Worksheet_Change(ByVal Target As Range)
Dim Zeile2, Zeile1 As Long
Set Target = Intersect(Target, Range("J5:J1005"))
If Target Is Nothing Then Exit Sub
If Target = "O" Or Target = "P" Or Target = "D" Then
Zeile2 = Target.Row
Zeile1 = 5
Do While Cells(Zeile2, 1) >= Sheets("Action Items").Cells(Zeile1, 1)
Zeile1 = Zeile1 + 1
Loop
Sheets("Action Items").Cells(Zeile1, 1).EntireRow.Insert
Range(Cells(Zeile2, 1), Cells(Zeile2, 14)).Copy _
Destination:=Sheets("Action Items").Cells(Zeile1, 1).End(xlUp)
Target.EntireRow.Delete
End If
End Sub
的代码(然后类似.End(xlUp)
),但没有任何效果到目前为止。
我做错了什么?
(很遗憾,我无法与您共享Excel文件,因为这是商业机密。)
答案 0 :(得分:0)
<强>替换强>
If Target = "O" Or Target = "P" Or Target = "D" Then
Zeile2 = Target.Row
Zeile1 = 5
Do While Cells(Zeile2, 1) >= Sheets("Action Items").Cells(Zeile1, 1)
Zeile1 = Zeile1 + 1
Loop
Sheets("Action Items").Cells(Zeile1, 1).EntireRow.Insert
Range(Cells(Zeile2, 1), Cells(Zeile2, 14)).Copy _
Destination:=Sheets("Action Items").Cells(Zeile1, 1).End(xlUp)
Target.EntireRow.Delete
End If
<强>与强>
If Target = "O" Or Target = "P" Or Target = "D" Then
Application.EnableEvents = False
Zeile2 = Target.Row
Zeile1 = 5
Do While Cells(Zeile2, 1) >= Sheets("Action Items").Cells(Zeile1, 1)
Zeile1 = Zeile1 + 1
Loop
Sheets("Action Items").Cells(Zeile1, 1).EntireRow.Insert
Range(Cells(Zeile2, 1), Cells(Zeile2, 14)).Copy _
Destination:=Sheets("Action Items").Cells(Zeile1, 1).End(xlUp)
Target.EntireRow.Delete
Application.EnableEvents = True
End If
可能还有其他问题。
答案 1 :(得分:0)
我终于弄明白了错误是什么!
删除.End(xlUp)
为我做了一件事,产生了以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Zeile2, Zeile1 As Long
Set Target = Intersect(Target, Range("J5:J1005"))
If Target Is Nothing Then Exit Sub
If Target = "O" Or Target = "P" Or Target = "D" Then
Zeile2 = Target.Row
Zeile1 = 5
Do While Cells(Zeile2, 1) >= Sheets("Action Items").Cells(Zeile1, 1)
Zeile1 = Zeile1 + 1
Loop
Sheets("Action Items").Cells(Zeile1, 1).EntireRow.Insert
Range(Cells(Zeile2, 1), Cells(Zeile2, 14)).Copy _
Destination:=Sheets("Action Items").Cells(Zeile1, 1)
Target.EntireRow.Delete
End If
End Sub