Excel VBA- choose two sheets from dynamic path , compare multiple columns and update difference

Viewed 29

I am new in VBA Macros I need your suggestions and recommendations. I want to automate my daily tasks as I have two worksheets so I pick two different excel sheets from different paths and compare their columns.

In sheet 1 (which file is used for compression) and sheet 2 (Which all data is available and used for read purpose). in Sheet1 I have 14 columns and in sheet2 I have 103 columns. I just want to compare specific 2 columns which are common in both. and difference will save in a new file.

I just wanted to know what is best way to solve this problem.

Here I paste code which compares two columns in two different sheets.

Sub ComparesheetsOwnCode()   
 
 RemoveCellFillColor 'this function will remove the color in sheet1
   
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
ColEvalueSh1 = sh1.Range("E" & Rows.Count).End(xlUp).Row '
ColBvalueSh2 = sh2.Range("B" & Rows.Count).End(xlUp).Row  
Set myRange = sh2.Range("B2", "B" & ColBvalueSh2)
ColBvalueSh2 = sh2.Range("B" & Rows.Count).End(xlUp).Row  'count the Records of Column B in Sheets 2
ColDvalueSh2 = sh2.Range("D" & Rows.Count).End(xlUp).Row 'count the Records of Column D in Sheets 2
Set myRange = sh2.Range("B2", "B" & ColBvalueSh2)
Set myRange1 = sh2.Range("D2", "D" & ColDvalueSh2)
lastRow1 = sh1.Cells(sh1.Rows.Count, "E").End(xlUp).Row
lastRow2 = sh2.Cells(sh2.Rows.Count, "B").End(xlUp).Row
Set compareRange = sh1.Range("E2:E" & lastRow1)
Set toCompare = sh2.Range("B2:B" & lastRow2)
'used for check the green Entreis 
  For i = 2 To lastRow2 '541
    For j = 2 To lastRow1 '373
    'to check the Green values in sheet
    If Application.WorksheetFunction.CountIf(myRange, sh1.Cells(i, 5)) = 
            0 Then
           Else
        'to check the Orange values in sheet
        If sh2.Cells(i, 2) = sh1.Cells(j, 5) And sh2.Cells(i, 4) <> sh1.Cells(j, 7) Then
        'If StrComp(sh2.Cells(i, 2), sh1.Cells(j, 5), vbBinaryCompare) And Not StrComp(sh2.Cells(i, 4), sh1.Cells(j, 7), vbBinaryCompare) Then
        sh1.Cells(i, 5).Interior.Color = rgbOrange
            Exit For
        End If
        End If
    Next j
Next i

End Sub

But issue is the Green Condition is working but when i going to check orange condition which uses And clause is not giving desired output in above code i allocate sheets as Sh1 and sh2

I want to the user will choose by own from any button (Only excel files) and whatever he files choose so only compare those files. so how to use Input function and process these same functionalities which i performed above.

TIA

0 Answers
Related