繁体   English   中英

将范围从一个工作表复制到另一个工作表的最后一行数据

[英]Copy a Range From One Worksheet To The Last Row of Data On Another

我想复制“B”列中的一系列单元格,并将其粘贴到同一列中同一工作簿的另一张工作表上的最后一行数据之后。 我有以下代码,但我不知道如何实际粘贴数据。

Dim lastRow As String

Sheet9.Range("B2:B100").Select
Sheet9.Range(Selection,Selection.End(xlDown)).Select
Selection.Copy


lastRow = Sheet14.Cells(Rows.Count, "B").End(xlUp).Row + 1
Sheet14.Range("B" & lastRow).Activate
Sheet14.Paste

请尝试下一个代码行:

Sheet9.Range("B2:B" & Sheet9.Range("B100").End(xlDown).Row).Copy _
    Destination:=Sheet14.Range("B" & Sheet14.Range("B" & Rows.count).End(xlUp).Row + 1)

它将表格 B2 复制到 B100 之后的最后一个空行...

复制范围

  • 要了解为什么不推荐第一个代码,请通读以下“经典”: 如何避免在 Excel VBA 中使用 Select

  • 第二个代码显示了如何分三个步骤复制范围:定义CopyRange ,定义PasteCell ( PasteRange ),最后是Copy/Paste

  • 为了更好地理解第二个代码,请查看 Microsoft 的Range.CopyRange.PasteSpecial本教程这些视频

  • 要使代码适用于您的情况,请将Sheet1替换为Sheet9 ,将Sheet2替换为Sheet14 ,将B10替换为B100

编码

Option Explicit

Sub CopyPasteSelectActivate()
    
    ' A quick fix, not recommended. It's a poorly improved version of what
    ' the macro recorder what produce.
    ' Rather use one of the 3 ways below.
    
    Dim LastRow As Long ' In this case 'As String' works, too.
    
    Sheet1.Activate
    Sheet1.Range("B2:B10").Select
    Sheet1.Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    
    LastRow = Sheet2.Cells(Rows.Count, "B").End(xlUp).Row + 1
    Sheet2.Activate
    Sheet2.Range("B" & LastRow).Activate
    Sheet2.Paste

End Sub

Sub CopyPaste3Ways()
    
' Define CopyRange.
    
    Dim CopyRange As Range
    
    ' Either...
    'Set CopyRange = Sheet1.Range("B2", Sheet1.Range("B2:B10").End(xlDown))
    ' ...or (I prefer the following 'xlUp' approach):
    Dim LastRow As Long
    LastRow = Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row
    Set CopyRange = Sheet1.Range("B2:B" & LastRow)
    
    Debug.Print "CopyRange:", Sheet1.Name, CopyRange.Address
    
' Define PasteCell.
    
    Dim FirstEmptyRow As Long
    FirstEmptyRow = Sheet2.Cells(Sheet2.Rows.Count, "B").End(xlUp).Row + 1
    
    Dim PasteCell As Range
    Set PasteCell = Sheet2.Range("B" & FirstEmptyRow)
    
    Debug.Print "PasteCell:", Sheet2.Name, PasteCell.Address
    
' The CopyPaste
    
    ' 1st Version
    ' This version is the most efficient of these 3 versions, but it can
    ' only be used for copying values (not formulas, formats... etc.).
    
    ' Either...
    'PasteCell.Resize(CopyRange.Rows.Count, CopyRange.Columns.Count).Value _
      = CopyRange.Value
    
    ' ...or:
    ' To better understand it, you can define a 'PasteRange'.
    Dim PasteRange As Range
    Set PasteRange = PasteCell.Resize(CopyRange.Rows.Count, _
                                      CopyRange.Columns.Count)
    PasteRange.Value = CopyRange.Value
    Debug.Print "PasteRange:", Sheet2.Name, PasteRange.Address
    
'    ' 2nd Version
'    ' This version will copy 'everything', including formulas, formats etc.

'    ' The following line...
'    CopyRange.Copy PasteCell

'    ' is just short for:
'    CopyRange.Copy Destination:=PasteCell

'    ' 3rd Version
'    ' This version will do the same as the previous, but as a downside,
'    ' it will change the selection on the 'PasteSheet' to 'PasteRange' and
'    ' will set the 'CutCopyMode' to 'xlCopy'. But as an upside, you can
'    ' choose what you want to copy by 'adding arguments' to 'PasteSpecial'.
'    ' Put the cursor right behind 'PasteSpecial' and press 'SPACE' to
'    ' see the various arguments and their parameters.

'    CopyRange.Copy
'    PasteCell.PasteSpecial
'    Application.CutCopyMode = False
    
End Sub

暂无
暂无

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

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