vba – 复制列标题以外的所有数据

我正在search人员工作簿中的一列,并将其复制到rota工作簿中的下一个空白行。 我遇到的问题是它也复制列标题(第一行)。 根据下面的代码,关于如何排除包含列标题的第一行的任何build议?

' find the column in the people workbook name = WorksheetFunction.Match("name", people.Sheets("Open").Rows(1), 0) num = WorksheetFunction.Match("num", people.Sheets("Open").Rows(1), 0) 'find the next empty row Dim lastrow As Integer lastrow = rota.Sheets("Offer").Range("A" & Rows.Count).End(xlUp).Row + 1 ' copy from the people workbook into the next free space in rota workbook people.Sheets("Open").Columns(name).Copy Destination:=rota.Sheets("Offer").Range("A" & Rows.Count).End(xlUp).Offset(1) people.Sheets("Open").Columns(num).Copy Destination:=rota.Sheets("Offer").Range("B" & lastrow) 

我猜在下面的点,我需要指定不复制第一行,而不是复制名为“num”的完整列…

 people.Sheets("Open").Columns(num).Copy Destination:=rota.Sheets("Offer").Range("B" & lastrow) 

为了尽可能多地保留它,我build议使用IntersectUsedRangeOffset 。 只要改变你的最后一部分:

 ' copy from the people workbook into the next free space in rota workbook With people.Sheets("Open") Intersect(.Columns(Name), .UsedRange.Offset(1)).Copy Destination:=rota.Sheets("Offer").Range("A" & Rows.Count).End(xlUp).Offset(1) Intersect(.Columns(num), .UsedRange.Offset(1)).Copy Destination:=rota.Sheets("Offer").Range("B" & lastrow) End With 

这确实是

 .Columns(num).Copy 

这是采取头,由于它复制整个列(因此“.Columns()。复制”)的事实。

我会build议对数组使用不同的方法来复制和粘贴工作表rota中的数据:

 Dim arrayCopied(), lastline as long ' find the column in the people workbook Name = WorksheetFunction.Match("name", people.Sheets("Open").Rows(1), 0) num = WorksheetFunction.Match("num", people.Sheets("Open").Rows(1), 0) 'Loading the data from sheet Open in the array ' We load the array from the second line : .cells(2,name) With people.Sheets("Open") arrayCopied = Range(.Cells(2, Name), .Cells(2, num).End(xlDown)) End With ' Then we paste the data in the corresponding place in the rota sheet With rota.Sheets("Offer") 'First we calculate where the last row is: lastline = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown)).Rows.Count 'Then we paste the array starting at the last line and finishing at the last line '+ the number of lines of the corresponding copied array - 1 (because Excel cells start at 1 and not at 0) .Range(.Cells(lastline, 1), .Cells(lastline + UBound(arrayCopied, 1) - 1, 2)) = arrayCopied End With 

这应该做的伎俩。