2017-07-17 5 views
-1

「シートA」と「シートB」の2枚のシートがあり、「シートA」にはいくつかのデータが含まれており、「シートB」にはすべてのデータが含まれています。だから私は "シートA"は、シートBに存在しない私はメッセージを表示したい任意のデータを持っていない場合。2つの列を比較してエラーメッセージを表示するMicrosoft VBA

VBAコード

Private Sub CommandButton1_Click() 
Dim myRng As Range 
    Dim lastCell As Long 

    'Get the last row 
    Dim lastRow As Integer 
    lastRow = ActiveSheet.UsedRange.Rows.Count 

    'Debug.Print "Last Row is " & lastRow 

    Dim c As Range 
    Dim d As Range 

    Application.ScreenUpdating = False 

    For Each c In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells 
     For Each d In Worksheets("Sheet2").Range("A2:A" & lastRow).Cells 
      c.Interior.Color = vbRed 
      If (InStr(1, d, c, 1) > 0) Then 
       c.Interior.Color = vbWhite 
       Exit For 
      End If 
     Next 
    Next 

    For Each c In Worksheets("Sheet2").Range("A2:A" & lastRow).Cells 
     For Each d In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells 
      c.Interior.Color = vbRed 
      If (InStr(1, d, c, 1) > 0) Then 
       c.Interior.Color = vbWhite 
       Exit For 
      End If 
     Next 
    Next 

Application.ScreenUpdating = True 
End Sub 
+2

どうしたのですか?私たちに推測するように求めてはいけません。 –

+1

vlookupを試してみてください。 – Jonnyboi

+0

や 'countif'や配列式が多分でしょう。これが内容か内容の一部であるかどうかはあなたが言っていません。 '' instr'は123ABC456のABCであるかもしれないが、確かではないことを意味する。何、いつ、どこで、他の背景が役立つかの表示。 –

答えて

0

あなたは、列ベースのデータを持っている場合は、この式を試みることができます。これはあなたのデータの横のシートA(A1はあなたのデータ、B1は数式です)とシートAの列AをシートBのそれ(matchRange =シートB、列x)で検索します。

=IF(ISNUMBER(MATCH(LookupValue,MatchRange,0)),"Yes","No")

関連する問題