【问题标题】:Copy Cell values from One Sheet to Another on certain criteria with the paste使用粘贴根据特定条件将单元格值从一张表复制到另一张表
【发布时间】:2015-01-17 06:59:35
【问题描述】:

您好,我正在尝试制作一个宏,它将从 Sheet1 复制所有 200 个值,从 A1 到 A2 ,并将它们粘贴到 Sheet 2 中,但粘贴操作应该以不同的方式将它们粘贴到 2 列和 3 行空间中,例如例子。

Sheet1(A1) => Sheet2(A1)
Sheet1(A2) => Sheet2(B1)

Sheet1(A3) => Sheet2(A5)
Sheet1(A4) => Sheet2(B5)    

Sheet1(A5) => Sheet2(A9)
Sheet1(A6) => Sheet2(B9)

如您所见,间隔为 +3 个单元格。 到目前为止,我所做的是复制单个单元格,并且需要任何帮助或建议如何从上面进行复制过程。

Sub CopySelection()
  Dim xlSel As Excel.Range
  Set xlSel = Excel.Application.Selection
  For i = 0 To 200
  xlSel.copy Excel.Application.Sheets("Sheet2").Range("A1")
  Next i
End Sub

欢迎任何帮助。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    你可以这样做:

    Dim Destination_Row As Integer
    Destination_Row = 1
    For Origin_Row = 1 To 200 Step 2
        Sheet2.Cells(Destination_Row, 1).Value = Sheet1.Cells(Origin_Row, 1).Value
        Sheet2.Cells(Destination_Row, 2).Value = Sheet1.Cells(Origin_Row + 1, 1).Value
        Destination_Row = Destination_Row + 4
    Next
    

    Cells 属性为指定的RowColumn 返回一个RangeFor 循环的每一步都加 2 以从 Sheet1 中获取值,并将 Destination_Row 加 4 以将值粘贴到 Sheet2

    【讨论】:

    • 它有效,唯一的问题是,如果我在 sheet1 中将属性更改为过去,它不会粘贴到 Sheet2 中,但它不适用于 sheet2
    • 我不明白你的意思,为什么它不起作用?
    • 未指定文件中正确的工作表,现在一切正常,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2020-01-13
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多