简体   繁体   中英

VBA - Compare Cell Values In Multiple Work Sheets

I've tasked myself with building a small QA tool, but the problem is that I'm not terribly familiar with VBA or programming. I want to be able to iterate through a single column within three different .csv files. The cells in these columns should match, and the whole point of the tool is to identify the cells that are the "odd man out".

Currently I have some spaghetti code that's giving me an error "false.xlsx" not found...please be gentle...

Sub CompareLists()

Dim count As Integer
Dim kRange As Range
Dim LastRow As Long
Dim iRow As Long
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim varSheetC As Variant


LastRow = Range("A" & Rows.count).End(xlUp).Row
Set kRange = ActiveSheet.Range("A3:A" & LastRow)


Set File_Path1 = Workbooks.Open(Filename = "C:\Test Files\Test_File1.csv")
Set varSheetA = File_Path1.Worksheets("Sheet1").Range(kRange)
Set File_Path2 = Workbooks.Open(Filename = "C:\Test Files\Test_File2.csv")
Set varSheetB = File_Path2.Worksheets("Sheet1").Range(kRange)
Set File_Path3 = Workbooks.Open(Filename = "C:\Test Files\Test_File3.csv")
Set varSheetC = File_Path3.Worksheets("Sheet1").Range(kRange)


For iRow = LBound(varSheetA) To UBound(varSheetA)
    count = 1
    If varSheetA(iRow) = varSheetB(iRow) Then
        count = count + 1
    Else
    If varSheetA(iRow) = varSheetC(iRow) Then
        count = count + 1
    Else
        If count < 3 Then
            ActiveCell.Interior.ColorIndex = 3
        End If
    End If
    End If
Next

End Sub

Your line:

Set File_Path1 = Workbooks.Open(Filename = "C:\Test Files\Test_File1.csv")

Evaluates the inside as a boolean check of whether some nonexistent Filename variable has the value "C:\\Test Files\\Test_File1.csv". Since the new variable Filename has no value, the comparison returns false. You were probably trying to do := and not = . The := operator assigns to a method parameter while = either does a boolean evaluation or variable assignment. Any one of the following four lines should fix your error as the first use the := operator to assign to the Filename parameter and the last two just know that Filename is the default first parameter.

Set File_Path1 = Workbooks.Open(Filename:="C:\Test Files\Test_File1.csv")
Set File_Path1 = Workbooks.Open Filename:="C:\Test Files\Test_File1.csv"
Set File_Path1 = Workbooks.Open("C:\Test Files\Test_File1.csv")
Set File_Path1 = Workbooks.Open "C:\Test Files\Test_File1.csv"

As a side note, this is something to be very careful about in VBA. Put Option Explicit at the top of every module so that it forces you to define your variables. If you do that, your erroring out line would have complained that variable 'Filename' was not defined.

As far as your next line goes:

Set varSheetA = File_Path1.Worksheets("Sheet1").Range(kRange)

you have two problems. First, when opening a csv file the tab name is always the same as the filename and NOT Sheet1. Second, kRange is part of the worksheet you started on so to get the comparable range on the new sheet you should use kRange.Address . Those fixes change the above into:

Set varSheetA = File_Path1.Worksheets("Test_File1").Range(kRange.Address)

Your loop treats ranges like arrays. The most direct way to fix that is to force them to be arrays by changing the Dim statements (adding parentheses) and the assignment statements for your range variables (removing Set and adding .Value ).

Dim varSheetA() As Variant
...
varSheetA = File_Path1.Worksheets("Test_File1").Range(kRange.Address).Value
...
If varSheetA(iRow, 1) = varSheetB(iRow, 1) Then

The best alternative method is to never create the range variables in the first place and just grab cells by row and column indices.

Sub CompareLists_2()
    Dim count As Integer
    Dim LastRow As Long, iRow As Long
    Dim MainSht As Worksheet, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet

    Set MainSht = ActiveSheet
    LastRow = MainSht.Range("A" & Rows.count).End(xlUp).Row
    Set ws1 = Workbooks.Open(Filename:="C:\Test Files\Test_File1.csv").Worksheets("Test_File1")
    Set ws2 = Workbooks.Open(Filename:="C:\Test Files\Test_File2.csv").Worksheets("Test_File2")
    Set ws3 = Workbooks.Open(Filename:="C:\Test Files\Test_File3.csv").Worksheets("Test_File3")

    For iRow = 3 To LastRow
        count = 1
        If ws1.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value Then
            count = count + 1
        Else
            If ws1.Cells(iRow, 1).Value = ws3.Cells(iRow, 1).Value Then
                count = count + 1
            Else
                If count < 3 Then
                    MainSht.Cells(iRow, 1).Interior.ColorIndex = 3
                End If
            End If
        End If
    Next
End Sub

Of course as far as I can tell your boolean logic reduces to:

If ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value And ws2.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
    MainSht.Cells(iRow, 1).Interior.ColorIndex = 3
End If

Revision of boolean logic per comments (though I'm slightly confused on the logic):

If ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value And ws2.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
    ws1.Cells(iRow, 1).Interior.ColorIndex = 3
ElseIf ws1.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
    ws1.Cells(iRow, 1).Interior.ColorIndex = 3
    ws2.Cells(iRow, 1).Interior.ColorIndex = 3
End If
Sub CompareLists()
Dim count As Integer
Dim LastRow As Long, iRow As Long
Dim MainSht As Worksheet, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
Dim ws5 As Workbook
Dim var1 As Variant


Set ws1 = Workbooks.Open(Filename:="C:\Test Files\Test_File1.csv").Worksheets("Test_File1")
Set ws2 = Workbooks.Open(Filename:="C:\Test Files\Test_File2.csv").Worksheets("Test_File2")
Set ws3 = Workbooks.Open(Filename:="C:\Test Files\Test_File3.csv").Worksheets("Test_File3")

Set MainSht = ws1
LastRow = MainSht.Range("A" & Rows.count).End(xlUp).Row

For iRow = 3 To LastRow
    count = 1

    If ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
        ws1.Cells(iRow, 1).Interior.ColorIndex = 3

    ElseIf ws1.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
        ws1.Cells(iRow, 1).Interior.ColorIndex = 6
        ws2.Cells(iRow, 1).Interior.ColorIndex = 6

    ElseIf ws1.Cells(iRow, 1).Value = ws3.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value Then
        ws1.Cells(iRow, 1).Interior.ColorIndex = 6
        ws3.Cells(iRow, 1).Interior.ColorIndex = 6
    End If

    If ws3.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws3.Cells(iRow, 1).Value <> ws1.Cells(iRow, 1).Value Then
        ws3.Cells(iRow, 1).Interior.ColorIndex = 3
    ElseIf ws3.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value And ws3.Cells(iRow, 1).Value <> ws1.Cells(iRow, 1).Value Then
        ws3.Cells(iRow, 1).Interior.ColorIndex = 6
        ws2.Cells(iRow, 1).Interior.ColorIndex = 6
    End If

    If ws2.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value And ws2.Cells(iRow, 1).Value <> ws1.Cells(iRow, 1).Value Then
        ws2.Cells(iRow, 1).Interior.ColorIndex = 3

    End If


Next



End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM