2016-06-21 13 views
1

ブックのブック内の列を検索し、ロータのブックにコピーします。私が持っている問題は、それも(最初の行)列ヘッダーをコピーしているということです。以下のコードに基づいて、列ヘッダーを含む最初の行を除外する方法に関する提案はありますか?vba - 列のタイトル以外のすべてのデータをコピーします。

私は「NUM」という名前の列全体をコピーするのではなく、最初の行をコピーしないように指定する必要があります以下の点でその推測を
' 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) 

イム...

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

チェックこのアウト:http://superuser.com/questions/42304/select-an-entire-column-minus-header-row-in-an-excel-macroだけにあなたの最後の部分を変更します – Vityata

答えて

3

そのまま残すには、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 
+0

私はそれが必要なように正確に働いた、ありがとう –

+0

あなたは歓迎です:) –

0

それは確か

です
.Columns(num).Copy 

これは、ヘッダーを取ります。これは、列全体をコピーするためです( ".Columns()。Copy")。私は&をコピーするために、配列とは異なるアプローチを使用することをお勧めし

は、ワークシートロタにデータを貼り付けます。

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 

これはトリックを行う必要があります。

+0

私は好奇心が強いですから* * "なぜデータをコピー&ペーストするために配列を使用することを提案しますか?" * –

+0

上記の答えはフォーマットを変更せずに行う必要があったのですが、回答ありがとう –

+0

問題はありませんAaron C. こんにちはDirk Reichelさん、よく私はかなり扱いやすいので、コピー&ペーストの処理の間に計算をするのが便利で、やや高速です(初期データを保存している間も列を追加できます)。 しかし、私はあなたの解決策に気を取っています。それはすばらしいことですが、その上ではもっと実践的なアプローチです。 –

関連する問題