2017-07-01 11 views
0

異なるワークシートの2つの列を比較する必要があります。2つの異なるシートから2つの列を比較する

第一シート(500行)

ColumnA(NUMBER)ColumnB(STRING)ColumnC(NUMBER)ColumnD(NUMBER)ColumnE(NUMBER)ColumnF(NUMBER)

セカンドシート

ColumnA私は2番目のshからシート3 SITESID(COLUMNA)でプリントアウトしたいアウトColumnF

と(NUMBER)ColumnB(STRING)ColumnC(NUMBER)ColumnD(NUMBER)ColumnE(NUMBER) 最初のシートには存在しません。

Option Explicit 
Sub Compare() 

Dim Row1Crnt As Long 
Dim Row2Crnt As Long 
Dim Row3Crnt As Long  
Dim Row1Last As Long 
Dim Row2Last As Long  

Dim ValueSheet1 
Dim ValueSheet2 
Dim duplicate As Boolean  
Dim maxColmn As Long 
Dim i 
maxColmn = 10 ' number of column to compare 
For i = 1 To maxColmn 

With Sheets("Sheet1") 
    Row1Last = .Cells(Rows.Count, i).End(xlUp).Row 
End With 

With Sheets("Sheet2") 
    Row2Last = .Cells(Rows.Count, i).End(xlUp).Row 
End With 

Row1Crnt = 2 
Row2Crnt = 2 
Row3Crnt = 2  
maxColmn = 10 

Do While Row2Crnt <= Row2Last 

duplicate = False 
Row1Crnt = 2 

With Sheets("Sheet2") 
ValueSheet2 = .Cells(Row2Crnt, i).Value 
End With 
Do While Row1Crnt <= Row1Last 

With Sheets("Sheet1") 
ValueSheet1 = .Cells(Row1Crnt, i).Value 
End With 

If ValueSheet1 = ValueSheet2 Then 
    duplicate = True 
Exit Do 

End If 
Row1Crnt = Row1Crnt + 1 
Loop 

If duplicate = False Then 
With Sheets("Sheet3") 
    .Cells(Row3Crnt, i).Value = ValueSheet2 
    Row3Crnt = Row3Crnt + 1 
    End With 

End If 

Row2Crnt = Row2Crnt + 1 
Loop 
Next 

End Sub 

しかし、私は、結果としてそれを取るよ すべてcolumnA(SITESID)最初のシート

+0

歓迎ですが、[ツアー](https://stackoverflow.com/tour)に慣れていない方のタグウィキをお読みください。 – pnuts

+0

@Gowthanお願い: – pnuts

答えて

0

最速を「が存在を確認するには存在しない第二のシートからのSheet2とColumnB(NAMES) 'はapplication.matchです。

sub compare 
    dim a as long, arr as variant, chk as variant 

    with worksheets("sheet1") 
     arr = .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup)).value2 
    end with 

    with worksheets("sheet3") 
     for a = lbound(arr, 1) to ubound(arr, 1) 
      if iserror(application.match(arr(a, 1), worksheets("sheet2").columns("A"), 0)) then 
       .cells(.rows.count, "A").end(xlup).offset(1, 0) = arr(a, 1) 
      end if 
     next a 
    end with 
end sub 
+0

あなたの答えはありがたいです、これは私の仕事ではなく、おそらく私は最初のコードを変更する必要があります。私がしたいのは、sheet1とsheet2を列Aに基づいて比較することで、sheet3では、2番目のスライドにあるデータを表示し、最初の行には存在しません。最初のコードは、他の列を含まず、ソートせずに、sheet2から列B(名前)がsheet2に存在し、最初の列には存在しません。手伝ってくれますか? –

関連する問題