简体   繁体   English

Excel VBA复制/粘贴范围从1个工作表到另一个,每次相同的范围粘贴到下一个目标列

[英]Excel VBA copy/paste range from 1 worksheet to another, same range pastes in next destination column each time

I am a new to Excel VBA. 我是Excel VBA的新手。

I am trying to copy a range of data from one worksheet into another worksheet in the same workbook. 我正在尝试将一个工作表中的一系列数据复制到同一工作簿中的另一个工作表中。

So copy range("A2:B10") in Sheet1 then paste in ("A2") in Sheet2. 因此,在Sheet1中复制range(“ A2:B10”),然后在Sheet2中粘贴(“ A2”)。

Each time the macro is run, I would like the values in ("Sheet 1").Range("A2:B10") to be pasted in the consecutive column, so in this instance in "B2" in Sheet 2. 每次运行宏时,我都希望将(“ Sheet 1”)。Range(“ A2:B10”)中的值粘贴到连续的列中,因此在本例中是在Sheet 2中的“ B2”中。

Thanks for your help! 谢谢你的帮助!

I have the first part, but struggling with the 2nd part: 我有第一部分,但为第二部分而苦苦挣扎:

Sub sbCopyRangeToAnotherSheet()
'Set range
Sheets("Sheet1").Range("A2:B10").Copy  Destination:=Sheets("Sheet2").Range("A2")
'Copy the data
Sheets("Sheet1").Range("A2:B10").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Range("A2").Select
'Paste in the target destination
ActiveSheet.Paste

Application.CutCopyMode = False
End Sub

Your code is odd 您的代码很奇怪

This line Sheets("Sheet1").Range("A2:B10").Copy Destination:=Sheets("Sheet2").Range("A2") is copying and pasting all in one line. 此行Sheets("Sheet1").Range("A2:B10").Copy Destination:=Sheets("Sheet2").Range("A2")将所有内容复制并粘贴到一行中。 So your subsequent lines where you .copy .activate .select and .paste are redundant. 因此,您在.copy .activate .select.paste之后的行是多余的。 Your first line already did all of this. 您的第一行已经完成了所有这一切。

You need to identify the last used column in Row "A" of Sheet2. 您需要标识Sheet2的行“ A”中最后使用的列。 To do this you can use the .End() method of a range to find the first occupied cell in the row. 为此,可以使用范围的.End()方法查找该行中的第一个占用的单元格。 Then use that cell as your copy Destination : 然后将该单元格用作复制Destination

 Sub sbCopyRangeToAnotherSheet()

     'Declare a variable to hold the cell to which we will paste the data
     Dim pasteCell as Range

     'Set the variable to the first empty cell in Row 2
     Set pasteCell = Sheets("Sheet2").Range("IV2").end(xlToLeft).Offset(,1)

     'Copy and paste
     Sheets("Sheet1").Range("A2:B10").Copy  Destination:=pasteCell
End Sub

This is something that will do the job without using Active and Select : 这是无需使用ActiveSelect即可完成的工作:

Option Explicit

Sub TestMe()

    Dim ws1         As Worksheet
    Dim ws2         As Worksheet        
    Dim source      As Range
    Dim target      As Range        
    Dim lastColumn  As Long

    Set ws1 = Worksheets(1)
    Set ws2 = Worksheets(2)

    With ws2
        lastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
        If WorksheetFunction.CountA(.Columns(1)) > 0 Then
            lastColumn = lastColumn + 1
        End If
    End With

    Set source = ws1.Range("A2:A10")
    Set target = ws2.Cells(2, lastColumn)

    source.Copy destination:=target
    Application.CutCopyMode = False

End Sub

The trickiest part in the code is to start writing on the correct column on the target worksheet. 代码中最棘手的部分是开始在目标工作表的正确列上编写。 If the worksheet is empty, then the correct column is the first one. 如果工作表为空,则正确的列是第一列。 In every other case, the correct column is the last column + 1. 在其他情况下,正确的列是最后一列+ 1。

The standard way of finding last used column - Range.End(xlToLeft).Column would always return the first column as a last used column, w/o paying attention whether it is used or not. 查找最后使用的列的标准方法Range.End(xlToLeft).Column始终将第一列作为最后使用的列返回,而无需注意是否使用了它。 Thus you need some kind of a check, to know whether it is empty or not: WorksheetFunction.CountA(.Columns(1)) > 0 因此,您需要进行某种检查以了解其是否为空: WorksheetFunction.CountA(.Columns(1)) > 0

暂无
暂无

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

相关问题 Excel VBA-“复制/粘贴”仅粘贴选定范围的值 - Excel VBA - “Copy/Paste” pastes only value of selected range 来自多个工作表的VBA Excel复制/粘贴特定范围 - VBA Excel Copy/Paste Specific Range from multiple worksheet excel vba-特定的复制/粘贴字符串到另一个工作表,其所有范围都在另一个工作表中的特定范围 - excel vba - specific copy/paste string to another sheet with all its range to specific range in another worksheet 复制并粘贴每个工作表中的范围 - Copy and paste a range from each worksheet 如何选择组合框值的行,然后使用带有VBA Excel的命令按钮将其复制并粘贴到另一个工作表范围 - how to select the row of combobox value then copy and paste to another worksheet range using command button with VBA Excel 在VBA Excel中复制粘贴范围 - copy paste Range in VBA Excel VBA将基于条件从命名范围复制到另一个工作表中的粘贴Vlaues - VBA to Copy from Named Range based on Condition to Paste Vlaues in another worksheet 如何根据VBA中的预定义索引从另一个工作表复制和粘贴一系列单元格 - How to copy and paste a range of cells from another worksheet depending on predefined indices in VBA 将动态范围从一个工作表复制并粘贴到另一个工作表 - Copy and Paste Dynamic Range from One Worksheet to Another Excel VBA-如果在日期范围内,将表行复制到另一个工作表 - Excel VBA - Copy Table Rows to another worksheet if within date range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM