简体   繁体   English

将多张工作表中的 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

I am relative novice in VBA and my goal is to automatically copy one column (B) from 3 named sheets (source sheets) and paste them in a new sheet and them repeat the process for column C and so on until a defined column (see image for my goal, in this case I wanted until column D of the source sheets).我是 VBA 的相对新手,我的目标是自动从 3 个命名工作表(源工作表)复制一列(B)并将它们粘贴到新工作表中,然后重复 C 列的过程,依此类推,直到定义的列(见我的目标图像,在这种情况下,我想要直到源工作表的 D 列)。 The structure of all sheets is identical.所有工作表的结构都是相同的。 Columns consist of numeric values.列由数值组成。

I have tried to write a code (see below) however I am getting run-time error 1004 for the commented line.我试图编写代码(见下文)但是我收到注释行的运行时错误 1004。 Also, not sure if the code will do what I want to.另外,不确定代码是否会做我想做的事。 What am I doing wrong and any tips to improve it?我做错了什么以及改进它的任何提示?

Sub CopyColumns3()

Dim sheetNames As Variant
sheetNames = Array("temp_column", "normalized_column", "derivative_column")

Dim columnLetters As Variant
columnLetters = Array("B", "C", "D")

Dim i As Integer
Dim j As Integer

' Create a new sheet after the last sheet in the workbook
sheets.Add After:=sheets(sheets.Count)

' Set the name of the new sheet
sheets(sheets.Count).Name = "A_final"

For i = 0 To UBound(sheetNames)
    For j = 0 To UBound(columnLetters)
        sheets(sheetNames(i)).columns(columnLetters(j)).Copy

        ' Check if there are any empty columns in the Destination sheet
        If sheets("A_final").Range("A1").End(xlToRight).Column = 256 Then
            ' If there are no empty columns, add a new column to the end of the sheet
            sheets("A_final").columns(sheets("A_final").columns.Count).EntireColumn.Insert
        End If

        sheets("A_final").Select
        ' The next line causes the problem
        sheets("A_final").Range("A1").End(xlToRight).Offset(0, 1).PasteSpecial
    Next j
Next i

End Sub

enter image description here在此处输入图像描述

Copy Columns复制列

Sub ColumnsToNewSheet()

    ' Define constants.
    
    Const DST_NAME As String = "A_final"
    Const DST_FIRST_COLUMN As String = "A"
    
    Dim sNames(): sNames = Array( _
        "temp_column", "normalized_column", "derivative_column")
    Dim sColumns(): sColumns = Array("B", "C", "D")

    ' Reference the workbook.
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Add a new sheet, rename it and reference the first Destination column.
    Dim dws As Worksheet
    Set dws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
    dws.Name = DST_NAME
    Dim drg As Range: Set drg = dws.Columns(DST_FIRST_COLUMN)
    
    ' Copy the Source columns to the Destination columns.
    ' This does sh1-col1,sh2-col1,sh3-col3... as requested.
    ' If you need sh1-col1,sh1-col2,sh1-col3... switch the loops.
    
    Dim srg As Range, sName, sColumn
    
    For Each sColumn In sColumns
        For Each sName In sNames
            Set srg = wb.Sheets(sName).Columns(sColumn)
            srg.Copy drg
            Set drg = drg.Offset(, 1) ' next Destination column
        Next sName
    Next sColumn

    ' Inform.
    MsgBox "Columns exported.", vbInformation

End Sub

I do not see why should you have that check for column 256.我不明白你为什么要检查第 256 列。

However, when it is triggered, you call Range.Insert , which clears the CutCopyMode .但是,当它被触发时,您调用Range.Insert ,它会清除CutCopyMode Therefore, Range.PasteSpecial fails because there is nothing to paste.因此, Range.PasteSpecial失败,因为没有可粘贴的内容。

You can move the check before the Range.Copy call, or get rid of it altogether.您可以在Range.Copy调用之前移动检查,或者完全摆脱它。

暂无
暂无

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

相关问题 将多个工作表中的列数据复制到同一列中的一个工作表,不会覆盖 - Copy column data from multiple sheets to one sheet in the same column, no overwriting 如何使用列名从一张工作表复制数据并粘贴到具有相同列名的另一张工作表? - How to copy data from one sheet using column name and paste to another sheet with same column name? 使用 VBA 将多张工作表中的列复制到同一工作簿中的一张工作表中 - Copying columns from multiple sheets into one sheet in the same workbook using VBA 使用相同的列名(不必相同的顺序)将数据从一张纸复制到另一张纸(同一工作簿) - Copy data from one sheet to another (same workbook) with same column names (not necessarily same order) Excel VBA复制/粘贴列从多个工作表到不同列中的一个工作表 - Excel VBA copy/paste column from multiple sheets to one sheet in different Columns 根据列 header 将数据从一张工作表传输到同一工作簿中的其他多张工作表 - transfer data from 1 sheet to other multiple sheets in same workbook based on column header 从一张纸上复制数据并将其从另一张相同的工作簿中过去 - copy data from one sheet and past it on another sheet of same workbook 将多张纸中的同一行复制到Excel中的一张纸中 - copy the same row from multiple sheets into one sheet in excel 将多张纸(但不是全部纸)的值复制/粘贴到一张纸中 - Copy/paste values from multiple sheets, but not all sheets, into one sheet 根据工作表名称将一列从一张工作表复制到多张工作表 - Copy a column from one sheet to multiple sheets based on sheetname
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM