简体   繁体   English

如何在两张Excel工作簿之间将数据从一列复制并粘贴到另一张……而不会覆盖目标列的内容。

[英]how to copy & paste the data from one column to another between two sheets of excel workbook…without overwriting the destination column content..?

how to copy & paste the data from one column to another between two sheets of excel workbook ... without overwriting the destination column content? 如何在两张Excel工作簿之间将数据从一列复制并粘贴到另一列... ...而不会覆盖目标列的内容?

I am using below code to copy & paste but every time I run it it is overwriting the existed content. 我正在使用以下代码进行复制和粘贴,但是每次运行它都会覆盖现有内容。 I want to be pasted from next row of the column. 我想从列的下一行粘贴。

Sub DirectCopySample()

    Application.ScreenUpdating = False
    Sheets("Updating Sheet").Range("A:A").Copy Destination:=Sheets("Sheet1").Range("G:G")
    Sheets("Updating Sheet").Range("B:B").Copy Destination:=Sheets("Sheet1").Range("F:F")
    Sheets("Updating Sheet").Range("C:C").Copy Destination:=Sheets("Sheet1").Range("B:B")
    Application.ScreenUpdating = True

End Sub

Don't copy the entire column. 不要复制整个列。 Copy a specific 1-cell-wide range of X rows (where X is your data) and define all your variables based on the current size of the data. 复制特定的1单元格范围的X行(其中X是您的数据),并根据当前数据大小定义所有变量。 For instance if you want to copy column A from sheet1 to the end of column B in sheet2. 例如,如果您要将A列从sheet1复制到sheet2中B列的末尾。

Sub CopyColumn()

Dim wsCopy As Worksheet
Set wsCopy = Sheets("<Sheet Name>")

Dim wsPaste As Worksheet
Set wsPaste = sheets("<Sheet Name>")

'/ Much better to make your worksheets variables and then reference those

Dim lngFirstRow As Long
Dim lngFinalRow As Long

Dim lngCopyColumn As Long
Dim lngPasteColumn As Long

Dim rngCopy As Range
Dim rngPasteCell As Range

    lngCopyColumn = 1 '/ ("A" Column)
    lngDestinationColumn = 2 '/ ("B" Column)

    wsCopy.Activate

        lngFirstRow = 1
        lngFinalRow = Cells(1048576, lngCopyColumn).End(xlUp).Row 
        '/ Starts at the bottom of the sheet, stops at the first cell with data in it, returns that cell's row

        Set rngCopy = Range(Cells(lngFirstRow, lngCopyColumn), Cells(lngFinalRow, lngCopyColumn))
            '/ Defines the range between those 2 cells
            rngCopy.copy

    wsPaste.Activate

        lngFinalRow = Cells(1048576, lngPasteColumn).End(xlUp).Row
        Set rngpaste = Cells(lngFinalRow + 1, lngPasteColumn)
        '/ Pastes to the row 1 cell below the last filed cell in Column B
        rngpaste.Paste

End Sub

@Grade 'Eh' Bacon outlined the correct process in his or her comment. @Grade'Eh'Bacon在他或她的评论中概述了正确的过程。

The crux of the issue is finding the size of the ranges you are copying from and pasting to. 问题的症结在于查找要复制和粘贴到的范围的大小。 My current favorite method of doing so is the code snippet below: 我目前最喜欢的方法是以下代码片段:

copyLastrow = Sheets("Updating Sheet").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

That will find the last non-empty row in your worksheet. 这将在工作表中找到最后一个非空行。 So if for some reason column A has 100 rows, B has 200 rows, and C has 300 rows it will return 300 as the last row. 因此,如果由于某种原因A列有100行,B列有200行,C列有300行,它将返回300作为最后一行。

On the paste side of things, you could use the same method and add 1 to it so you paste into the first empty row, but if the columns have different numbers of rows you will end up with many blank rows in the shorter columns before your data is pasted at the bottom. 在粘贴方面,您可以使用相同的方法并向其添加1,以便粘贴到第一个空行中,但是如果列的行数不同,则在结束之前,较短的列中将有许多空白行数据粘贴在底部。

A work around this is the following code: 解决此问题的方法是以下代码:

 pasteLastrowG = Sheets("Sheet1").Range("G" & Rows.Count).End(xlUp).Row + 1

This will start at the bottom of column G and head up until it hits a row with data in it and then add 1 so that you are pasting into the first blank row of the column. 它将从G列的底部开始,一直向上直到它碰到其中包含数据的行,然后加1,以便您粘贴到该列的第一空白行。 You could then create variables for columns H and I that do the same thing. 然后,您可以为H和I列创建相同功能的变量。

Putting it all together your code would look something like this in the end: 将所有代码放在一起,最终看起来像这样:

copyLastrow = Sheets("Updating Sheet").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

pasteLastrowG = Sheets("Sheet1").Range("G" & Rows.Count).End(xlUp).Row + 1
'pasteLastrowH ...
'pasteLastrowI ...

Sheets("Updating Sheet").Range("A2:A" & copyLastrow).Copy Destination:=Sheets("Sheet1").Range("G" & pasteLastrowG)
'Copy and paste B code here
'Copy and paste C code here

暂无
暂无

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

相关问题 将一列从一个工作簿复制并粘贴到另一个 - Copy and paste a column from one workbook to another 如何将数据从一列移动到另一列而不覆盖目标列中的现有值 - How to move data from one column to another without overwriting existing values in destination column Excel VBA-将两列从一个工作簿复制并粘贴到另一个工作簿 - Excel VBA - Copy and Paste two columns from one workbook into another 在Excel中将数据从一列移动到另一列,而无需复制/剪切和粘贴 - Movng data in Excel from one column to another without copy/cut & paste 用于在工作簿中的多个工作表上复制和粘贴最后一列数据的 VBA 脚本 - VBA Script to copy and Paste last column of data on multiple sheets in a workbook 从另一个工作簿中提取特定的列并复制到目标工作簿 - Extract a particular column from another workbook and copy to the destination workbook Excel-VBA:将列值从一个工作簿复制到另一个工作簿 - Excel-VBA: to copy column value from one workbook to another Excel宏从一个工作簿中的两个工作表中的两个单元格复制到另一个 - Excel macro to copy from two cells from two sheets in one workbook to another 将多个工作表中的列数据复制到同一列中的一个工作表,不会覆盖 - Copy column data from multiple sheets to one sheet in the same column, no overwriting 将多张工作表中的 1 列复制到同一工作簿中的一张工作表中,然后从同一张最终工作表中复制/粘贴第二列 - Copying 1 column from multiple sheets into one sheet in the same workbook and them copy/paste a 2nd column from the same final sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM