【发布时间】: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 解释清楚。 ?