VBA复制并粘贴到新工作表

时间:2014-07-31 13:29:26

标签: vba excel-vba excel

以下代码基本上以与Sheet2相同的顺序从Sheet1复制并粘贴到Sheet2。我需要它将值粘贴到Sheet2上的相同列中,以便我可以对所有值运行排序。我似乎无法弄清楚如何将它粘贴到Sheet2上的同一列。谢谢。

Sub InfoSharing()

Dim lastrowDB As Long, lastrow As Long
Dim array1, array2, i As Integer

With Sheets("Sheet1")
    lastrowDB = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With

array1 = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
array2 = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")

For i = LBound(array1) To UBound(array1)
    With Sheets("Sheet1")
        lastrow = Application.Max(3, .Cells(.Rows.Count, array1(i)).End(xlUp).Row)
        .Range(.Cells(3, array1(i)), .Cells(lastrow, array1(i))).Copy
        Sheets("Sheet2").Range(array2(i) & lastrowDB).PasteSpecial xlPasteValues
    End With

Next
Application.CutCopyMode = False

End Sub

2 个答案:

答案 0 :(得分:0)

如果你想翻转价值

,我认为你不能使用复制/粘贴

您可以设置每个单元格的value属性。 您需要跟踪目标表中A列中的当前行。 然后遍历正在处理的列的行,并从源单元格的value属性设置value属性。

答案 1 :(得分:0)

您的任务很容易,只需看下面的内容:

Dim i As Byte, y As Byte, x As Integer

For i = 3 To 5 'i is a number of column counts from left to right in Sheet1 to be copied

    For y = 3 To 17 'y is a number of row in Sheet1 to be copied

        x = x + 1 'x is a number of row in Sheet2 to be pasted

        Sheets("Sheet2").Cells(x, 1).Value = Sheets("Sheet1").Cells(y, i).Value

    Next y

Next i

我为你的应对问题做了一个嵌套循环。它可以根据需要添加多列(调整 i 变量),通过所需的行数(调整 y 变量)并将所有内容粘贴到Sheet2中的A列(一个按一,使用 a 变量作为行号。

如果您需要更多内容,请随时在评论中提问。