2017-08-18 6 views
0

一部のシート(マスター、1,2,3)でファイルを取得しました。私は他のシートから私のマスターファイルのデータにコピーする必要がありますが、すべての エクセルの最初に見つける必要があり ID1 ID2市OLTストリート番号他のシートの列を見つけ、データループをコピーします

ためにある -

あなたが私のマスターシートに列を見ることができるの下

他のシートのID1列(その混乱 - 順序どおりではない)と行をコピーします。 1.シート1の列ID1(A1)を見つけ、マスターシートA2にデータをコピーします。 2.シート1の列ID2(B1)を検索し、マスタB2にコピーします。その他 ループで行う方法?

サブcopy_data iは(マスタファイルA1から:Q1)の列を検索するためのコードをmodyfyする必要がある()

Dim b As Range 
     Dim lastrow As Integer 
lastrow = Cells(Rows.Count, 3).End(xlUp).Row + 1 

Set b = Sheets("1").Rows(1).Find("ID1") 

If b Is Nothing Then 

    MsgBox "ID1 not found", vbInformation, "Goods not found" 

Else 

    Set b = Range(b.Offset(1), b.Offset(Rows.Count - 1).End(xlUp)) 
    b.Copy Destination:=Sheets("master").Range("A" & lastrow) 

End If 
'id 
Dim wrb As Range 
Set wrb = Sheets("1").Rows(1).Find("ID2") 

If wrb Is Nothing Then 

    MsgBox "id2 not found", vbInformation, "Goods not found" 

Else 

    Set wrb = Range(wrb.Offset(1), wrb.Offset(Rows.Count - 1).End(xlUp)) 
    wrb.Copy Destination:=Sheets("master").Range("B" & lastrow) 
    End If 
End Sub 

他のシートおよびシートをマスターにデータをコピーします。それをループする方法です。ヘルプ

答えて

0

ため

おかげで以下の変更をしようと、これは出力を期待されたものであるなら、私に知らせてください。

Sub copy_data() 


    Dim b As Range 
    Dim lastrow As Integer 

    x = ThisWorkbook.Sheets.Count 'added 
    j = 1 
    Do While j <> x 


lastrow = Cells(Rows.Count, 3).End(xlUp).Row + 1 
lastrowM = Sheets("master").Cells(Rows.Count, 2).End(xlUp).Row + 1 'added 

Set b = Sheets(j).Rows(1).Find("ID1") 

If b Is Nothing Then 

    MsgBox "ID1 not found", vbInformation, "Goods not found" 

Else 

    Set b = Range(b.Offset(1), b.Offset(Rows.Count - 1).End(xlUp)) 
    b.Copy Destination:=Sheets("master").Range("A" & lastrowM) 

End If 
'id 
Dim wrb As Range 
Set wrb = Sheets(j).Rows(1).Find("ID2") 

If wrb Is Nothing Then 

    MsgBox "id2 not found", vbInformation, "Goods not found" 

Else 

    Set wrb = Range(wrb.Offset(1), wrb.Offset(Rows.Count - 1).End(xlUp)) 
    wrb.Copy Destination:=Sheets("master").Range("B" & lastrowM) 
    End If 
    j = j + 1 
    Loop 
End Sub 
関連する問題