【发布时间】:2013-09-26 22:23:26
【问题描述】:
我开发了以下代码来比较 A 列和 D 列中的两个单元格(字符串),如果发现部分匹配,则在相应的 B 单元格中记下 D 单元格值。
Sub CompareAndGuess()
Dim strLen, aux As Integer
Dim max1, max2 As Long
Dim str As String
Range("A1").Select
Selection.End(xlDown).Select
max1 = ActiveCell.Row
Range("D1").Select
Selection.End(xlDown).Select
max2 = ActiveCell.Row
For a = 2 To max1
str = Cells(a, 1)
str = StrConv(str, vbUpperCase)
strLen = Len(str)
aux = strLen
For l = 3 To strLen
For d = 2 To max2
If Cells(d, 4) = Left(str, aux) Then
Cells(a, 2) = Cells(d, 4)
Exit For
ElseIf Cells(d, 4) = Right(str, aux) Then
Cells(a, 2) = Cells(d, 4)
Exit For
End If
Next d
aux = aux - 1
If Cells(a, 2) <> "" Then
Exit For
End If
Next l
Cells(a, 2).Select
Next a
End Sub
谁能帮我找出问题出在哪里,因为当我运行它时,代码只能猜对 50 行中的一行,而它应该至少匹配 40 左右。
拜托,我真的找不到那里的错误。如果您愿意,请随时为我的问题提出另一种解决方案。
我正在分析的数据样本是: 有错别字的名字:-
Jatiuca
Pajuara
Poco
Santa Luzia
Pajucara
Domingos Acacio
Jaragua
Stella Maris
P Verde
Tabuleiro dos Martin
Gruta Lourdes
Brasilia
Centro Historico
Monumento
Tabuleiro dos Martins
要在此列表中搜索的拼写错误名称:-
JARAGUÁ
TABULEIRO DO MARTINS
CENTRO
BRASÍLIA
CACIMBAS
JATIÚCA
CAITITUS
PAJUÇARA
CANAÃ
PONTA VERDE
CANAFÍSTULA
POÇO
CAPIATÃ
CAVACO
SANTA LÚCIA
【问题讨论】:
-
VB 有
InStr函数,这里可能会感兴趣... -
是的,我也考虑过这种方法,感谢您的提醒。我会试一试,但我仍然想知道我的代码有什么问题......
-
因为与1(一)相似?如果是这样,好点。谢谢!
-
您能否提供任何具有预期结果的样本数据
-
伙计们,我找到了正确的方法。我会在这里发帖只是为了向大家和任何在谷歌上搜索的人展示。再次感谢大家!虽然我自己认为该方法非常有用(特别是“InStr”提示!!!)