【发布时间】:2019-05-22 09:12:07
【问题描述】:
我正在尝试放置一个宏,允许我将相同的条目从一个表匹配到另一个表。棘手的部分是,如果找到匹配项,则无法重复。我对其进行理论化的方式有点初级,但鉴于我对 VBA 的了解仍然有限,这是我能想到的唯一方式。
结构
- 需要先过滤两个表才能允许不重复条件。
- 将搜索值存储为数组以加快宏的处理速度
- 将要搜索的条目与目标表中的条目进行匹配,以便找到匹配项。这是通过应用程序内函数 MATCH 完成的。 MATCH 函数返回匹配所在的单元格,这很有用,因为它会不断移动范围,以免一直重复相同的值。
- 计算移位范围后,我使用 VLookup 函数返回第二个条目。
很遗憾,宏不完整。我无法找到一种在不损害机制的情况下不断改变范围的方法。问题在于没有正确创建在每次比赛后进行移位的移位范围。
想要的结果
在下图中,期望的结果是检查左表中的所有项目是否都在右表中。拿项目A,我需要找到两个项目A。我在右栏中有一个值为 17 的第一个项目 A 和一个值为 81 的第二个项目 A。如果我没有找到任何值,我什么都没有,就像 Ds 和 E 的情况一样。如果相反,我的条目更少左表(与条目 L 的情况一样)然后我需要返回条目 L 的所有值:96; 77; 40.
Sub Matching11()
ThisWorkbook.Activate
Worksheets.add
Worksheets("Test4").Range("A1:T110").copy Destination:=ActiveSheet.Range("A1")
With ActiveSheet
Dim Search_Array As Variant
Search_Array = Range("C2", Range("C1").End(xlDown)) 'use this array to loop through the value to search for
Dim Target_MatchValue As Integer
Dim Target_Range As Range
Dim arr As Variant
Dim counter As Integer
Dim n As Integer
counter = 0
n = 0
Target_MatchValue = 0
For counter = LBound(Search_Array) To UBound(Search_Array)
Target_MatchValue = 0
Target_MatchValue = Application.Match(Search_Array(counter, 1), .Range("H2:H200"), 0) - 1 'change C column with the range where you will have the tyres you need search for
Set Target_Range = .Range(.Cells(2 + n, 8), .Cells(1000, 9)) 'this is supposed to work as a shifting range allowing to match entries without making repetitions. I used the MATCH function in order to set the start of the range. i.e. if there is a match in the target table the range will shift from the location of the match downwards. If the match is at on the same level then it does not shift the range in order to match the same-level entry afterwards it is supposed to shift by one unit in order to prevent repetitions.
'If arr = Application.VLookup(Search_Array(counter, 1), Target_Range, 2, False) Is Nothing Then GoTo NextCounter 'I used Vlookup in order to return the value set in the second column of the targetted table. As alternative, I think I could just use offset since I previously used MQTCH
arr = Application.VLookup(Search_Array(counter, 1), Target_Range, 2, False)
If IsError(arr) Then
GoTo NextCounter
Else
.Range(Cells(1 + counter, 6), Cells(1 + counter, 6)).value = arr 'Return the value of the array in this cell
End If
Target_Range.Select
If Target_MatchValue = 0 Then
n = n + 1
ElseIf Target_MatchValue > 0 Then
n = n + Target_MatchValue
End If
.Range(Cells(1 + counter, 5), Cells(1 + counter, 5)).value = Search_Array(counter, 1) 'Return the value of the array in this cell
Next counter
NextCounter:
Next counter
End With
End Sub
【问题讨论】:
-
我不太明白你的意思。您可能对 VLOOKUP() 和 HLOOKUP() 函数感兴趣。
-
Vlookup 总是返回相同的值
-
仔细阅读 vlookup() 的帮助。必须对数据进行排序,或者必须相应地设置第三个参数。带有键的列不应有重复的值。否则,结果不是你想要的。 Noo 算法可以决定您想要哪个值,如果您有两个与同一个键关联的值(A -> Row1,A -> Row2)。算法如何决定如何在 Row1 和 Row2 之间进行选择?
-
那么也许你应该使用 c1=h1, d1=i1, c2=h2, d2=i2... 等等......你只是用最基本的公式复制数据。跨度>
-
你的规范很弱。示例:为什么在 C 列中添加 2 行带有“D”的行?更疯狂:2行带“i”,1行10行,1行空……你需要能够用文字描述所有规则和所有异常,这样算法会更容易找到。
标签: arrays excel vba match vlookup