【发布时间】:2019-09-21 05:02:30
【问题描述】:
如果选择了正确的状态和名称,我正在尝试复制和粘贴 A:E 行。例如,如果状态在 A 列中显示“进行中”或在 B 列中显示名称“Thomas Xiong”。然后它将将该行复制并粘贴到另一个名为“WIPTX”的工作表中。在工作表“WIPTX”中,它将复制该行并将其粘贴到 I:M 列下的下一个可用行下。工作表 WIPTX 的以下列如下所示。
Status Columns
Assigned A:E
Accepted F:J
In Progress K:O
On Hold P:T
Completed U:Y
Cancelled Z:AD
我已经研究了很长一段时间,并测试了许多代码,看看它是否可以工作。我尝试的最后一个代码在 case "In Progress" 之后给了我一个语法错误。
Sub Hello()
Dim lRow As Long, cRow As Long, j As Long
With Sheets("WIPdata")
lRow = .Range("A800").End(xlUp).Row
' another method of finding last row in Column A (skipping blank cells in the middle)
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For j = lRow To 1 Step -1
cRow = Sheets("WIPTX").Range("A800").End(xlUp).Row
Select Case .Range("J" & j).Value
Case "Assigned"
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Cells(Sheets("WIPTX").Cells(Sheets("WIPTX").Rows.Count, "A").End(xlUp).Row + 1, "A")
Case "Accepted"
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Cells(Sheets("WIPTX").Cells(Sheets("WIPTX").Rows.Count, "E").End(xlUp).Row + 1, "E")
Case "In Progress"
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "I").End(xlUp).Row + 1, "I")
Case "On Hold"
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "M").End(xlUp).Row + 1, "M")
Case "Completed"
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "Q").End(xlUp).Row + 1, "Q")
Case "Cancelled"
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "U").End(xlUp).Row + 1, "U")
End Select
Next
End With
End Sub
【问题讨论】:
-
您的语法已关闭。我想你需要
.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Cells(Rows.Count, "I").End(xlUp).Offset(1) -
基本上应该是这样的?案例“进行中”.Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Cells(Rows.Count, "K").End(xlUp).Offset (1)
-
我的代码仍然什么也不做,它也没有粘贴到我需要的地方。
-
你需要澄清你正在尝试完成什么;您的声明
"paste the row to a different worksheet called, "WIPTX". In worksheet "WIPTX" it will then copy and paste that row under the next available row under Columns I:M."没有意义。此外,您的代码并未反映您在声明中所说的内容;您根本不测试 Col A 或 Col B。如果您提供前后产品的示例,将会有所帮助。如果您要测试不同列中的不同值,那么您还需要使用嵌套循环。您希望如何测试个人姓名? -
那么您认为 if then 语句可能会更好吗?比如说,如果 cell.value("A") = "Assigned" 和 cell.value("B") = "Thomas Xiong" 然后复制并粘贴到工作表 ("WIPTX")。类似的东西?