Results 1 to 4 of 4

Thread: VBA: Compare 2 cells from different workbook and publish the text right or wrong

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello mrfrid,

    The attached workbook has the macro shown below added to it with a button on "Sheet2" to run it. Let me know if this meets your needs.

    Sub CompareData()
    
        Dim j           As Long
        Dim k           As Long
        Dim MainData    As Variant
        Dim MainRng     As Range
        Dim MainWks     As Worksheet
        Dim Status      As Variant
        Dim TestData    As Variant
        Dim TestRng     As Range
        Dim TestWks     As Worksheet
        
            Set MainWks = Worksheets("Sheet1")
            Set TestWks = Worksheets("Sheet2")
            
            Set MainRng = MainWks.Range("A1").CurrentRegion
            Set TestRng = TestWks.Range("A1").CurrentRegion
            
            MainData = MainRng.Columns("B:D").Value
            TestData = TestRng.Columns("B:D").Value
            ReDim Status(1 To TestRng.Rows.Count, 1 To 2)
            
                For j = 1 To UBound(MainData)
                    For k = 1 To UBound(TestData)
                        If TestData(k, 1) = MainData(j, 1) Then
                            If TestData(k, 2) = MainData(j, 2) And TestData(k, 3) = MainData(j, 3) Then
                                Status(k, 1) = "Right Price"
                            Else
                                Status(k, 2) = "Wrong Price"
                            End If
                        End If
                    Next k
                Next j
                
            TestRng.Columns("G:H").Value = Status
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •