[英]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
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.