2016-09-02 19 views
1

私は2つの異なるワークシート(マスターワークシートは 'Stock09042016')のストックを比較し、新しいシートに新しい結果を生成しようとしています。マクロは、マスターワークシートからの量(F2)を2番目のワークシートと比較する必要があります。結果が異なる場合は、それらを他のすべての列とともに新しいシートにコピーする必要があります。結果と在庫が同じ場合は、新しいワークシートに追加してはいけません(これは私が紛失した場所です)。2つのワークシートをExcelでマクロと比較する

また、以下のコードは1つの行のみを行い、リスト全体にコピーしません。

ワークシートの例:マクロの

worksheet example

結果:

Result of macro

Sub RunMe() 
Dim lRow, lrow2 As Long 
Dim fValue As Range 

Sheets("STOCK09042016").Select 
lRow = Range("A1").End(xlDown).Row 
lrow2 = Sheets("STOCK26082016").Range("C1").End(xlDown).Row 

For Each cell In Range("A2:A" & lRow) 
    With Sheets("STOCK26082016").Range("C2:C" & lrow2) 
     Set fValue = .Find(cell.Value, LookIn:=xlValues) 
     If fValue Is Nothing Then 
     cell.EntireRow.Copy Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) 
     End If 
    End With 
Next cell 

End Sub 
+1

https://support.office.com/en-gb/article/What-you-can-do-with-Spreadsheet-Inquire-ebaf3d62-2af5-4cb1-af7d-e958cc5fad42は、関心のあるかもしれない – pnuts

答えて

0

これは、仕事をする必要があります。数量が異なる場合は、行を置き換えたいと思っていました。

Sub RunMe() 
Dim rCell As Range 
Dim rSource As Range 
Dim rTarget As Range 

With ThisWorkbook.Worksheets("STOCK26082016") 
    Set rSource = .Range("F2", .Cells(Rows.Count, 6).End(xlUp)) 
End With 

For Each rCell In rSource 
    With ThisWorkbook.Sheets(3) 

    'Try to find item 
    Set rTarget = .Range("B2", .Cells(Rows.Count, 2).End(xlUp)).Find(rCell.Offset(0, -4)) 

    'Check if entry for this item exist, if not copy entire row to the next empty row 
    If rTarget Is Nothing Then 
     rCell.EntireRow.Copy Destination:=.Range("A2", .Cells(Rows.Count, 2).End(xlUp)).Offset(1, 0) 

    'If it exist, check if the quantity matches. If not, replace the row 
    ElseIf Not rTarget.Offset(0, 4).Value = rCell.Value Then 
     rCell.EntireRow.Copy Destination:=rTarget.EntireRow 
    End If 
    End With 
Next rCell 

End Sub 
+0

ああ、それは仕事をした。助けてくれてありがとう! – Hursh

+0

@Hurshあなたの問題を解決した場合は、それをアップしてください;) – Chrowno

関連する問題