简体   繁体   English

VBA-使用变体的比较异常

[英]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. 我有非常简单的for循环进行比较。 The purpose is to color the Excel cell red if there isn't a match. 目的是在不匹配的情况下将Excel单元格涂成红色。 The results are 99% accurate, but I have noticed a couple of seemingly random errors. 结果是99%的准确度,但我注意到几个看似随机的错误。 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. 例如,包含数字104875的单元格没有被涂成红色,这表示在比较列中应该有一个匹配的单元格。 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. 我是VBA的新手,仍在尝试了解它的工作原理。

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 〜facepalm〜这就是我对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. 例如,假设您要比较活动表中AB列的前50个单元格。 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. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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