简体   繁体   English

使用VBA将动态范围复制并粘贴到Excel中的新工作表

[英]Copy and Paste dynamic ranges to new sheet in Excel with VBA

I am new to macro writing and I need some help. 我是宏写作的新手,需要帮助。

I have one sheet and need to copy the columns and reorder them to paste into a software program. 我只有一张纸,需要复制这些列并重新排序以粘贴到软件程序中。

  1. I want to copy A2 - the last data entry in column A and paste it into A1 on Sheet2 我想复制A2-A列中的最后一个数据条目并将其粘贴到Sheet2的A1中
  2. I want to copy B2 - the last data entry in column A and paste it into K1 on Sheet2 我想复制B2-A列中的最后一个数据条目并将其粘贴到Sheet2的K1中
  3. I want to copy C2 - the last data entry in column A and paste it into C1 on Sheet2 我想复制C2-A列中的最后一个数据条目并将其粘贴到Sheet2的C1中
  4. I want to copy D2 - the last data entry in column A and paste it into D1 on Sheet2 我要复制D2-A列中的最后一个数据条目并将其粘贴到Sheet2的D1中
  5. Then from Sheet 2, I want to copy A1:KXXXX (to the last entry in column A) and save it on the clipboard to paste into the other application 然后从工作表2中,我要复制A1:KXXXX(到A列的最后一个条目)并将其保存在剪贴板上以粘贴到其他应用程序中

Here is my code, I have tried... (I know this is just for copying column A, but I got stuck there.) 这是我的代码,我已经尝试过...(我知道这只是用于复制列A,但是我被卡在那里了。)

Sub Copy()
    aLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Range("A2" & aLastRow).Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A1").Select
    ActiveSheet.Paste
End Sub

Thank you so much for your help! 非常感谢你的帮助! Jess 枷锁

Try this instead. 试试这个吧。 Given that you said you got an error with the paste code and I am still using that, I think you'll still have an error there. 假设您说粘贴代码出错,而我仍在使用该代码,那么我认为您仍然会遇到错误。 Post the error message. 发布错误消息。 Hopefully we can figure that out. 希望我们能弄清楚。

Sub copyStuff()
    Dim wsIn As Worksheet
    Set wsIn = Application.Worksheets("Sheet1")

    Dim endRow As Long
    wsIn.Activate
    endRow = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row

    Dim r As Range
    Dim wsOut As Worksheet
    Set wsOut = Application.Worksheets("Sheet2")

    ' column a to column a
    Set r = wsIn.Range(Cells(2, 1), Cells(endRow, 1))
    r.Copy
    wsOut.Range("A1").PasteSpecial xlPasteAll

    ' column b to column k
    Set r = wsIn.Range(Cells(2, 2), Cells(endRow, 2))
    r.Copy
    wsOut.Range("K1").PasteSpecial xlPasteAll

    ' column c to column c
    Set r = wsIn.Range(Cells(2, 3), Cells(endRow, 3))
    r.Copy
    wsOut.Range("C1").PasteSpecial xlPasteAll

    ' column d to column d
    Set r = wsIn.Range(Cells(2, 4), Cells(endRow, 4))
    r.Copy
    wsOut.Range("D1").PasteSpecial xlPasteAll

    ' Copy data from sheet 2 into clipboard
    wsOut.Activate
    Set r = wsOut.Range(Cells(1, 1), Cells(endRow - 1, 11))
    r.Copy

End Sub

My original answer is below here. 我的原始答案在下面。 You can disregard. 您可以忽略。

This should accomplish your first goal: 这应该可以实现您的第一个目标:

Sub copyStuff()
    Dim wsIn As Worksheet
    Set wsIn = Application.Worksheets("Sheet1")

    Dim endRow As Long
    endRow = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row

    Dim r As range
    Set r = wsIn.range(Cells(2, 1), Cells(endRow, 4))
    r.Copy

    Dim wsOut As Worksheet
    Set wsOut = Application.Worksheets("Sheet2")

    wsOut.range("A1").PasteSpecial xlPasteAll

End Sub

I copied all 4 columns at once since that would be much faster but it assumes the columns are the same length. 我一次复制了所有4列,因为这样做会快得多,但是它假定这些列的长度相同。 If that isn't true you would need to copy one at a time. 如果不是这样,则需要一次复制一个。

The data should be in the clipboard at the end of the macro. 数据应位于宏末尾的剪贴板中。

Edit: I removed "wsIn.Activate" since it isn't really needed. 编辑:我删除了“ wsIn.Activate”,因为它并不是真正需要的。 Edit 2: Oops! 编辑2:糟糕! I just noticed you wanted the output in different columns. 我只是注意到您想要在不同的列中输出。 I'll work on it. 我会努力的。

Generally you want to avoid .Select and .Paste when copying values and rather copy by .value = .value : 通常,在复制值时要避免使用.Select.Paste ,而是使用.value = .value复制:

Sub Copy()
    Dim aLastRow As Long
    Dim clipboard As MSForms.DataObject
    Set clipboard = New MSForms.DataObject

    aLastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    Sheets("Sheet2").Range("A1:A" & aLastRow - 1).Value = Sheets("Sheet1").Range("A2:A" & aLastRow).Value
    Sheets("Sheet2").Range("K1:K" & aLastRow - 1).Value = Sheets("Sheet1").Range("B2:B" & aLastRow).Value
    Sheets("Sheet2").Range("C1:D" & aLastRow - 1).Value = Sheets("Sheet1").Range("C2:D" & aLastRow).Value

    clipboard.SetText Sheets("Sheet2").Range("A1:K" & aLastRow - 1).Value
    clipboard.PutInClipboard
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM