如何使用锯齿状数组从12 xl张中复制和粘贴特定列(如B,J,N,M,U,V),最后它必须存储在单独的单页中。
例如: 在Sheet1中我需要复制B,J,N,M 在Sheet2中,我需要复制B,J,N,O,U,V,X,AO 。 。 。 Upto Sheet12我需要像这个特定的列及其值,直到它的最后一行必须被复制,最后必须在新工作表的末尾粘贴12个工作表值。有人帮助我使用vba
答案 0 :(得分:0)
我要试试。这是这样的吗?根据需要更改代码,并根据需要为列索引设置更多数组。这会将所有这些列粘贴到新工作表上。
Sub test()
Dim a() As Variant
Dim wb As Workbook
Dim newWS As Worksheet
Dim nextColumn As Long, lastrow As Long, i As Long, o As Long
Set wb = ThisWorkbook
Set newWS = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count))
a = Array(1, 4, 6, 8, 10, 11) 'column indexes
With wb
nextColumn = 1
For i = 1 To 12
With .Worksheets(i)
For o = LBound(a) To UBound(a)
lastrow = .Cells(65536, a(o)).End(xlUp).Row
.Range(.Cells(1, a(o)), .Cells(lastrow, a(o))).Copy
newWS.Cells(1, nextColumn).PasteSpecial xlPasteAll
nextColumn = nextColumn + 1
Next o
End With
Next i
End With
End Sub