Excel – VBA:查找匹配时处理数组元素

我正在开发一个程序,其目的是识别数据库中的地址部分。 input是一个列,每个单元格包含地址中的一个字/数字。 另一方面,在数据库中,每个单元格包含几个单词的完整信息。

这里是一个例子: 在这里输入图像说明

这是我已经在做的事情:
1 /在数据库列(在这里从G3到G7)循环并激活当前单元格。
2 /对于(B2:B9)的每个单元格,查找与ActiveCell匹配
3 /如果find一个匹配的话,给一个单元格添加10个点,当B列的循环结束时,跳到数据库的另一个单元格。 所以在这个例子中,G3会有3个匹配,所以有30个点。

没关系,但是我想通过考虑“General Finance Tower”这个词的位置,将其视为与数据库匹配的位置,从而使它更加准确。

为此,我打算将数组中的单元格的内容拆分为G。

这是改进的方式:
1 /从G2:G7的数据库列中循环。 将第一个单元格拆分为n个单词(本例中为3个):“General / Finance / Tower”
2 /查找数组第一个单词列B中的元素之间的匹配。 如果不匹配,则跳到下一个元素(B2,B3,… B9)。 如果在B9之后仍然不匹配,那么跳到数组的第二个元素(Finance)并继续。
如果匹配(这里是“General”(数组的第一个元素)和B2)然后查看数组的下一个元素和B列的下一个元素 (“Finance”和“Finance”)是否匹配 。 如果是的话,再做一次(“塔”和“塔”)等等。

这样,“综合金融”将被发现,然后“综合金融大厦”,给我的计划更加准确。

这是我的问题,更多与编程有关:

我知道如何将G列拆分为数组,但我不知道如何导航。 如果不是一个数组,它是N个不同的单元格,我将开始到单元格1,激活它,然后使用偏移量(1,0)转到下一个单元格,偏移量(2,0)进一步转到两个单元格,等等,在每一个案件寻找火柴。 如何在使用数组的时候这样做? 如何去下一个元素?

stringData = Split(ActiveCell.Value, " ") For i = LBound(stringData) To UBound(stringData) If Match(ActiveCell, stringData(i)) Then ... else End If Next i 

这将允许我从第一个元素到最后一个,但是不会真正为我提供导航选项(例如,如果当前元素匹配,直接与第二个元素查找匹配项)。

预先感谢您的build议,这将真的帮助!

所以我写了你的代码,根据我对你的问题复杂性的理解得分。 input和输出如下所示: 输入和输出

和代码…
x)有许多意见,你应该能够很容易地修改它,以防万一不正确。

 Option Explicit Sub DatabaseVsInputComparison_Scoring() Dim ws As Worksheet ' worksheet instance Dim i&, j&, k&, x As Long ' iterators Dim db_startRow As Long ' --> Dim db_startColumn As Long ' --> These variables will Dim db_lastRow As Long ' --> store the database table Dim db_lastColumn As Long ' --> boundries Dim inp_startRow As Long ' starting row of the data in INPUT column Dim inp_lastRow As Long ' last row in the INPUT column Dim inp_column As Long ' the column number of the INPUT column Dim rng As Range ' active db range reference Dim inp_rng As Range ' active input ref Dim score As Long ' store temporary score ' // setters Set ws = Sheets("Sheet1") ' set reference db_startRow = 3 ' set starting row for the database data db_startColumn = 7 ' set starting column for the database data inp_startRow = 2 ' set starting row of the data in input column inp_column = 2 ' set starting row for the input column ' // getters ' get the boundries of the database table db_lastRow = ws.Cells(Rows.Count, db_startColumn).End(xlUp).Row db_lastColumn = ws.Cells(db_startRow, Columns.Count).End(xlToLeft).Column inp_lastRow = ws.Cells(Rows.Count, inp_column).End(xlUp).Row ' iterate through the database table For i = db_startRow To db_lastRow ' each ROW For j = db_startColumn To db_lastColumn ' each COLUMN score = 0 ' reset the score for each cell in the database set Set rng = ws.Cells(i, j) Dim splitted ' array storing each word of the "active" cell splitted = Split(rng.Value, " ") If UBound(splitted) > -1 Then For k = inp_startRow To inp_lastRow ' each input column data cell Set inp_rng = ws.Cells(k, inp_column) ' check if the first word has got a match in the input column If StrComp(CStr(splitted(0)), inp_rng.Value, 1) = 0 Then score = 12 ' set initial score ' this is where you want to iterate through the rest of the active database cell ' and check if the next words match, right? For x = 1 To UBound(splitted) ' now youre checking the next word in the splitted array ' against the next word in the input column If StrComp(CStr(splitted(x)), inp_rng.Offset(x, 0).Value, 1) = 0 Then ' if the match is found you want to keep on checking ' and incrementing the score score = score + 12 ' if no match you want to exit the loop ' > no extra score Else Exit For End If Next x End If Set inp_rng = Nothing Next k ' score calculation ' if max score reached then add extra 3 to the score If score = ((UBound(splitted) + 1) * 12) Then score = score + 3 rng.Offset(0, 5).Value = score Set rng = Nothing End If Next j Next i End Sub