Excel 通过项目拆分的vlookup不起作用
有一次,我问了一个类似vlookup的函数,但它是一个分割值。我用了很长时间。现在,代码似乎不再有效。 是什么原因导致代码不再工作Excel 通过项目拆分的vlookup不起作用,excel,split,vlookup,vba,Excel,Split,Vlookup,Vba,有一次,我问了一个类似vlookup的函数,但它是一个分割值。我用了很长时间。现在,代码似乎不再有效。 是什么原因导致代码不再工作 Sub test() Dim Cl As Range, Key As Variant Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary") Dic.CompareMode = vbTextCompare With Sheets("Sheet1") For Each Cl In .R
Sub test()
Dim Cl As Range, Key As Variant
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare
With Sheets("Sheet1")
For Each Cl In .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Row)
If Cl.Value <> "" Then
Dic.Add Cl.Row & "|" & Replace(LCase(Cl.Value), ";", "||") & "|", Cl.Offset(, 1).Text
End If
Next Cl
End With
With Sheets("Sheet2")
For Each Cl In .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Row)
For Each Key In Dic
If Key Like "*|" & LCase(Cl.Value) & "|*" And Cl.Value <> "" Then
Cl.Offset(, 1).Value = Dic(Key)
Exit For
End If
Next Key
Next Cl
End With
End Sub
子测试()
尺寸Cl作为范围,键作为变型
Dim Dic As Object:Set Dic=CreateObject(“Scripting.Dictionary”)
Dic.CompareMode=vbTextCompare
附页(“第1页”)
对于范围内的每个Cl(“A1:A”和.Cells.SpecialCells(xlCellTypeLastCell.Row)
如果Cl.值为“”,则
Dic.添加Cl.行和“|”并替换(LCase(Cl.Value),“;”,“||”)和“|”,Cl.Offset(,1).Text
如果结束
下一个Cl
以
附页(“第2页”)
对于范围内的每个Cl(“A1:A”和.Cells.SpecialCells(xlCellTypeLastCell.Row)
对于Dic中的每个键
如果像“*|”&LCase(Cl.Value)&“|*”和Cl.Value”这样的键,那么
Cl.偏移量(,1).值=Dic(键)
退出
如果结束
下一键
下一个Cl
以
端接头
目前没有错误,但代码不起作用。对一些人来说,这是有效的。对我来说不是。
请参见以下预期结果:
我觉得有必要重构您的代码,因为我刚刚发布了一个关于如何执行任务的答案 您能否编辑您的答案,以包含表1和表2 A列中的样本数据
Sub RefactoredCode()
Dim Cl As Range
Dim key, keys, results
Dim MatchString As String
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = vbTextCompare
With Sheets("Sheet1")
For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
dic.Add Cl.Row & "|" & Replace(LCase(Cl.Value), ";", "||") & "|", Cl.Offset(, 1).Text
Next Cl
End With
keys = dic.keys
With Sheets("Sheet2")
For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
MatchString = "|" & LCase(Cl.Value) & "|"
results = Filter(keys, MatchString, True, vbTextCompare)
If UBound(results) > -1 Then
key = results(0)
Cl.Offset(, 1).Value = dic(key)
End If
Next Cl
End With
End Sub
我运行了重构代码()
和测试()
。他们两个都工作正常
我觉得有必要重构您的代码,因为我刚刚发布了一个关于如何执行任务的答案 您能否编辑您的答案,以包含表1和表2 A列中的样本数据
Sub RefactoredCode()
Dim Cl As Range
Dim key, keys, results
Dim MatchString As String
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = vbTextCompare
With Sheets("Sheet1")
For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
dic.Add Cl.Row & "|" & Replace(LCase(Cl.Value), ";", "||") & "|", Cl.Offset(, 1).Text
Next Cl
End With
keys = dic.keys
With Sheets("Sheet2")
For Each Cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
MatchString = "|" & LCase(Cl.Value) & "|"
results = Filter(keys, MatchString, True, vbTextCompare)
If UBound(results) > -1 Then
key = results(0)
Cl.Offset(, 1).Value = dic(key)
End If
Next Cl
End With
End Sub
我运行了重构代码()
和测试()
。他们两个都工作正常
你的错误在哪里?您是否更改了输入数据?更重要的是,你的代码应该做什么?你的错误在哪里?您是否更改了输入数据?更重要的是,你们的代码应该做什么?我添加了一个预期结果的图像。我希望现在能更清楚。@user3114375我更新了我的答案。但我们的两个代码都按预期运行。谢谢您的回复。不幸的是,这两种方法都不起作用。对于您的代码,B1中的第二页显示1。仅此而已。我添加了预期结果的图像。我希望现在能更清楚。@user3114375我更新了我的答案。但我们的两个代码都按预期运行。谢谢您的回复。不幸的是,这两种方法都不起作用。对于您的代码,B1中的第二页显示1。这就是全部。