我正在人工作簿中搜索一列,并将其复制到 rota 工作簿中的下一个空白行。我遇到的问题是它也在复制列标题(第一行)。基于以下代码,有关如何排除包含列标题的第一行的任何建议吗?
' 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)
答案 0 :(得分:3)
为了保持原样,我建议您使用here,Intersect
和UsedRange
。只需将您的上一部分更改为:
' 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
答案 1 :(得分:0)
确实是
.Columns(num).Copy
正在取得标题,因为它复制整个列(因此" .Columns()。复制")。
我建议使用不同的数组方法来复制&将数据粘贴到工作表 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
这应该可以解决问题。