簡體   English   中英

VBA:循環從主表中復制某些列,創建新表並粘貼

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

我有一個帶有特定數字列的工作表。 我需要復制A列和從B列開始的另一列,創建一個新表並將這些列粘貼到那里。 然后,我想循環它,以便它這次復制A和C列,然后創建一個新工作表並粘貼,依此類推,直到到達主工作表的最后一列。 列A是固定的,因此總是被復制,復制的第二列是變化的列。 我在想這樣的事情

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

幫助將不勝感激。 謝謝。

最好的選擇是在Range()中使用Cells()以便遍歷每一列。 這段代碼應該可以幫助您:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM