简体   繁体   English

VBA:循环从主表中复制某些列,创建新表并粘贴

[英]VBA: loop to copy certain columns from main sheet, create a new sheet, and paste

I have a sheet with a specific number columns. 我有一个带有特定数字列的工作表。 I need to copy columns A and another column starting with column B, create a new sheet and paste those columns there. 我需要复制A列和从B列开始的另一列,创建一个新表并将这些列粘贴到那里。 I then want to loop it so that it copies columns A and C this time, then create a new sheet and paste, and so forth until it reaches the last column on the main sheet. 然后,我想循环它,以便它这次复制A和C列,然后创建一个新工作表并粘贴,依此类推,直到到达主工作表的最后一列。 Column A is fixed so that it is always copied, the second column copied is the one that varies. 列A是固定的,因此总是被复制,复制的第二列是变化的列。 I'm thinking something like this inside the loop 我在想这样的事情

Sheets(1).Activate
Range("A1:A14").Select
'This is where I need to copy the next column over and increment every time the code loops
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste

Help would be appreciated. 帮助将不胜感激。 Thank You. 谢谢。

Your best bet is to use Cells() inside of your Range() in order to loop through each column. 最好的选择是在Range()中使用Cells()以便遍历每一列。 This bit of code should help you out: 这段代码应该可以帮助您:

Sub columnCopy()

Dim sh1 As Worksheet
Set sh1 = sheets("Sheet1")

lc = sh1.Cells(1, Columns.Count).End(xlToLeft).Column ' Last Column

For i = 2 To lc

    sh1.Range(sh1.Cells(1, 1), sh1.Cells(12, 1)).Copy
    Worksheets.Add
    sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValues

    sh1.Range(sh1.Cells(1, i), sh1.Cells(12, i)).Copy
    sheets(1).Cells(1, 2).PasteSpecial Paste:=xlPasteValues

Next i

End Sub

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

相关问题 VBA将工作表的某些列复制并粘贴到另一张工作表上的特定列 - VBA Copy and Paste Certain Columns of a sheet to specific columns on another sheet 使用 VBA 将列复制并粘贴到新工作表 - Using VBA to Copy and Paste Columns to New sheet 如何从一张工作表复制一行并将仅某些列粘贴到新工作表? - How to copy a row from one sheet and paste only certain columns to a new sheet? VBA代码将某些数据从工作表复制并粘贴到另一个 - VBA code to copy and paste certain data from sheet to another 为多个工作表创建VBA,并使用公式将粘贴复制到新列中 - Create VBA for multiple sheet and copy paste in new column with formula VBA将粘贴列复制到其他工作表中 - VBA Copy paste columns in different sheet VBA如何从工作表中复制数据并将其粘贴到新工作簿 - VBA How to copy data from a sheet and paste to a new workbook 从一张工作表复制和转置数据并创建一个新工作表,然后将数据粘贴到新工作表中 - Copy & Transpose Data from one sheet & create a new sheet then paste the data into the new sheet 查找某些文本,复制并粘贴到新工作表中 - Find certain text, copy and paste into new sheet VBA 用于过滤的宏,从列中复制指定值并创建然后粘贴到具有该列名称的新工作表中 - VBA Macro to filter, copy the specified value from a column and create then paste in a new sheet with that column name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM