简体   繁体   中英

VBA - Comparison anomalies using variant

I have found a couple of other questions dealing with variants but none of them seem to address my issue.

I have very simple for loops doing comparisons. The purpose is to color the Excel cell red if there isn't a match. The results are 99% accurate, but I have noticed a couple of seemingly random errors. For example, a cell containing the number 104875 is not colored red, which indicates that there should be a matching cell in the comparison column. But there isn't. It seems like they should all be wrong or all be correct. Some of the other threads about variants have mentioned that the comparisons have to be of the same type or you will get weird errors. In my case, they are of the same type (both integers), so this isn't the problem.

I am brand new to VBA and still trying to understand how it works.

This is the relevant part of the code:

Private Sub CommandButton1_Click()

Dim i As Long, j As Long
Dim flag As Boolean
Dim array1() As Variant, array2() As Variant
Dim column1 As Double
Dim column2 As Double

column1 = convertColumn(TextBox1.Text)
column2 = convertColumn(TextBox2.Text)

Set wb1 = Workbooks("Advocate July 2017 Data.xlsm").Sheets(1)
Set wb2 = Workbooks("BI Report 8-18-17.xlsm").Sheets(1)

array1 = Intersect(wb1.Columns(column1), wb1.UsedRange)
array2 = Intersect(wb2.Columns(column2), wb2.UsedRange)


For i = 2 To UBound(array1)
    flag = False
    For j = 2 To UBound(array2)
        If IsNumeric(array1(i, 1)) And IsNumeric(array2(j, 1)) Then If CDbl(array1(i, 1)) = CDbl(array2(j, 1)) Then flag = True
        Next j
    If Not flag Then wb1.Cells(i, column1).Interior.Color = vbRed
Next i
End Sub

EDIT: Turns out that my code works fine. The problem was simply that some of the cells on one of the sheets were hidden and I didn't realize it. ~facepalm~ that's what I get for being inexperienced in excell

Try to simplify your code, to something easily reproductible. Eg, lets say that you want to compare the first 50 cells in columns A and B in the activesheet. Put some values and it will look like this:

Option Explicit

Public Sub TestMe()

    Dim array1      As Variant
    Dim array2      As Variant
    Dim i           As Long
    Dim j           As Long
    Dim flag        As Boolean

    With ActiveSheet
        array1 = .Range("A1:A50")
        array2 = .Range("B1:B50")
        .Range("A1:A10").Interior.Color = vbWhite

        For i = LBound(array1) To UBound(array1)
            flag = False

            For j = LBound(array2) To UBound(array2)
                If array1(i, 1) = array2(j, 1) Then flag = True
            Next j

            If Not flag Then .Cells(i, 1).Interior.Color = vbRed
        Next i

    End With

End Sub

Then try to adapt the solution to yours. It should work.

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