【问题标题】:Error in finding the exact match查找完全匹配时出错
【发布时间】:2017-07-12 18:29:30
【问题描述】:

我有三张工作表,Sh1、Sh2、Sh3。 Sh3 是我的结果表。

我已将 N 列从 Sh1 复制到 E 列中的 Sh3。

我考虑 sheet3 的 E 列,然后比较 sheet2 的 A 列,如果匹配,则将结果复制到 F 列的 sheet3 中。

我能够找到匹配的列,但是,我有一些特殊情况,我无法解决。

我已经在图片中解释了它们。

[![The Image Shows an example of how the ID Looks in column E of sht3][1]][1]

![Here i have shown both structure of ID. The first row id is the same, and has no Problem ist finding the match. In the second row, there is an 0 less in my sheet2, and the code Fails to Display that, in row 3, the Id has an 0 extra in sheet2. in row 4, i have an id with other id, but in sheet2, i have the same id, with 0 extra, same in row 5 as well, The ID are generally 11 to 13 Digit Long. ]1

有人可以建议我如何克服这个问题。下面是代码,我用于将值从一张表复制到另一张表并在另一张表中查找值。

Sub lookup()
Dim lLastrow As Long
Dim rng As Range
Dim i As Long

ThisWorkbook.Sheets("S").Select
lLastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
Range("P5:P" & lLastrow).Copy Destination:=Sheets("Result_APQP").Range("E5")
Range("G5:G" & lLastrow).Copy Destination:=Sheets("Result_APQP").Range("H5")

Sheets("Result").Select
For i = 5 To lLastrow

Set rng = Sheets("P").UsedRange.Find(Cells(i, 5).Value & "*", LookAt:=xlWhole)
'If it is found put its value on the destination sheet
If Not rng Is Nothing Then
Cells(i, 6).Value = rng.Value
 End If
Next i
End Sub

【问题讨论】:

  • 您能否更明确地解释一下哪些 ID 需要相等?据我了解,我们可以在比较 ID 时忽略 0,对吗?
  • 嗨,Mikz,您能多解释一下您在以下情况下期望发生的事情吗:1)工作表 2 的 ID 中有一个额外的零,2)的 ID 中的 ID 少了一个零表 2。还是复制功能正在删除零?
  • @Vlad 第一列中的 ID 是 sheet1 中的 ID,第二列中的 ID 是 sheet2 中的 ID,我正在寻找 sht1 中的 ID 以匹配 sht2 中的 ID。在我提到的情况下,它们失败了,我无法匹配 ID。
  • @maxhob17 不,复制功能无法识别零。尽管我的表格中的零或多或少,但考虑到它应该相互匹配的前 11 位数字,并且第 4 行和第 5 行是 spl 情况,在这种情况下,我也希望 ID 匹配并显示在 sheet3 中。
  • @maxhob17 解释清楚。 ?

标签: vba excel


【解决方案1】:

我会这样做:

Sub clear(rng As Range)
    For Each cell In rng.Cells
        cell.Value = Split(cell.Value, ",")(0)
        cell.Value = Split(cell.Value, ";")(0)
        While Mid(cell.Value, Len(cell.Value)) = "0"
            cell.Value = Mid(cell.Value, 1, Len(cell.Value) - 1)
        Wend
    Next cell
End Sub


Sub lookup()
    Dim lLastrow As Long
    Dim rng As Range
    Dim i As Long

    ThisWorkbook.Sheets("S").Select
    lLastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
    Range("P5:P" & lLastrow).Copy Destination:=Sheets("Result_APQP").Range("E5")
    Range("G5:G" & lLastrow).Copy Destination:=Sheets("Result_APQP").Range("H5")

    Call clear(Sheets("Result").UsedRange)
    Call clear(Sheets("P").UsedRange)

    Sheets("Result").Select
    For i = 5 To lLastrow

        Set rng = Sheets("P").UsedRange.Find(Cells(i, 5).Value & "*", LookAt:=xlWhole)
        'If it is found put its value on the destination sheet
        If Not rng Is Nothing Then
            Cells(i, 6).Value = rng.Value
        End If
    Next i
End Sub

注意,clear() 子程序会修改原始数据。

【讨论】:

  • 你没有提到范围或单元格,它是如何识别的?
  • 没有得到关于范围/单元格的问题
  • 在函数 clear 中,我的问题是我们没有提到像 cell (i,11).value 这样的东西。上面的代码如何识别它应该在哪里寻找?
  • 1) 该函数获取一个范围并修改该范围内的每个单元格。 2)关于超出范围的错误 - 在不知道确切的行和变量值的情况下很难调试它。在我的测试样本中,它可以正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 2011-12-19
  • 2014-05-07
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
相关资源
最近更新 更多