简体   繁体   English

比较两个不同列的值与excel VBA中的唯一条件

[英]comparing values of two different columns with a unique condition in excel VBA

I am trying to compare the values of two different columns. 我正在尝试比较两个不同列的值。

图片

so there are two cases:- 所以有两种情况:

  1. In column 'A' a two digit value is present which leads to a seven digit value in column 'B'. 在“ A”列中出现两位数的值,导致在“ B”列中出现七位数字的值。
  2. In column 'A' a three digit value is present which leads to a eight digit value in column 'B'. 在“ A”列中存在一个三位数的值,该值导致在“ B”列中具有八位数的值。

I want to validate whether 'B2' and 'B3' start with 14 and 21 respectively if no Msgbox 'Error in row number'. 如果没有Msgbox'行号错误',我想验证'B2'和'B3'是否分别以14和21开头。 and whether 'B3' and 'B4' start with and 109 and 289 respectively if no Msgbox 'Error in row number'. 如果没有Msgbox“行号错误”,则“ B3”和“ B4”是否分别以,109和289开头。

I would use a UDF. 我会使用UDF。 Enter this is a standard module: 输入这是一个标准模块:

Public Function IsItGood(r1 As Range, r2 As Range) As String
    Dim v1 As String, v2 As String, L As Long
    v1 = r1.Text
    v2 = r2.Text
    L = Len(v1)
    If v1 = Left(v2, L) Then
        IsItGood = "Noerror"
    Else
        IsItGood = "Error in this row"
    End If
End Function

Then in C2 enter: 然后在C2中输入:

=isitgood(A2,B2)

and copy down: 并抄下来:

在此处输入图片说明

You may give this a try... 您可以尝试一下...

Sub CompareColumns()
Dim lr As Long, n As Long
Dim rng As Range, cell As Range
Dim strA As String, strB As String, str As String
Dim NotMatched As Boolean

lr = Cells(Rows.Count, 1).End(xlUp).Row

'Assuming your data starts from Row2
Set rng = Range("B2:B" & lr)
str = "The following cells don't match." & vbNewLine & vbNewLine
For Each cell In rng
    If cell <> "" Then
        n = Len(cell.Offset(0, -1))
        If n > 0 Then
            strA = cell.Offset(0, -1).Text
            strB = Left(cell, n)
            If strA <> strB Then
                NotMatched = True
                str = str & cell.Offset(0, -1).Address(0, 0) & " : " & cell.Offset(0, -1).Value & vbTab & cell.Address(0, 0) & " : " & cell.Value & vbNewLine
            End If
        Else
            str = str & cell.Offset(0, -1).Address(0, 0) & " : " & cell.Offset(0, -1).Value & vbTab & cell.Address(0, 0) & " : " & cell.Value & vbNewLine
        End If
    End If
    n = 0
    strA = ""
    strB = ""
Next cell
If NotMatched Then
    MsgBox str, vbInformation
Else
    MsgBox "Both columns match.", vbInformation
End If
End Sub

Put this formula in C2 and copy Down: 将此公式放在C2中并向下复制:

=IF(AND(LEN(SUBSTITUTE(B2,A2,""))=5,FIND(A2,B2&A2)=1),"No Error","Error")

在此处输入图片说明


To get a list of row numbers that do not work you can use this formula: 要获取无效行号的列表,可以使用以下公式:

=AGGREGATE(15,6,ROW($A$2:$A$7)/((--LEFT($B$2:$B$7,LEN($A$2:$A$7))<>$A$2:$A$7)+(LEN($B$2:$B$7)-LEN($A$2:$A$7)<>5)>0),ROW(1:1))

Put it in the first cell then copy/paste down till you run out of rows with errors. 将其放在第一个单元格中,然后复制/粘贴下来,直到用完所有有错误的行。

在此处输入图片说明

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

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