【问题标题】:Copy and Paste to a different worksheet and column复制并粘贴到不同的工作表和列
【发布时间】: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")。类似的东西?

标签: excel vba for-loop


【解决方案1】:
Sub InProgress()

Dim idxprogress As Integer
idxprogress = 3

Dim idxassigned As Integer
idxassigned = 3

Dim idxonhold As Integer
idxonhold = 3

Dim idxcancelled As Integer
idxcancelled = 3

Dim idxaccepted As Integer
idxaccepted = 3

Dim idxcompleted As Integer
idxcompleted = 3


Sheets("WIPdata").Activate
For Each cell In Range("A2:A55")
Sheets("WIPdata").Activate
Select Case cell.Value

Case "In Progress"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxprogress, 11), Cells(idxprogress, 15)).PasteSpecial
idxprogress = idxprogress + 1

Case "Assigned"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxassigned, 1), Cells(idxassigned, 5)).PasteSpecial
idxassigned = idxassigned + 1

Case "On Hold"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxonhold, 16), Cells(idxonhold, 20)).PasteSpecial
idxonhold = idxonhold + 1

Case "Cancelled"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxcancelled, 26), Cells(idxcancelled, 30)).PasteSpecial
idxcancelled = idxcancelled + 1

Case "Accepted"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxaccepted, 6), Cells(idxaccepted, 10)).PasteSpecial
idxaccepted = idxaccepted + 1

Case "Completed"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxcompleted, 21), Cells(idxcompleted, 25)).PasteSpecial
idxcompleted = idxcompleted + 1
Case Else


End Select

Next cell

End Sub

【讨论】:

  • 我能够使用案例陈述来解决这个问题。如果有人需要使用与此类似的东西,您可以切换出工作表中的语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多