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