(请参阅上面附带的数据和输出)
这是我的示例数据。我要找的是每次在A列中找到D列中的单词,将A,B,C列中的值复制并粘贴到另一组列中,如下图所示。这是我写的代码
Sub stringfinder()
Dim term As String
Dim keyword As String
Dim finaltable As String
Dim clicks As Integer
Dim impressions As Integer
Dim cost As Integer
finaltable = 2
For i = 2 To 900000
For j = 2 To 50
If Range("A" & i).Value Like "*" & Range("G" & j) & "*" Then
term = Cells(i, 1).Value
clicks = Cells(i, 2).Value
impressions = Cells(i, 3).Value
cost = Cells(i, 4).Value
Cells(finaltable, 7).Value = term
Cells(finaltable, 8).Value = clicks
Cells(finaltable, 9).Value = impressions
Cells(finaltable, 10).Value = cost
finaltable = finaltable + 1
End If
Next j
Next i
End Sub
对于像州和进步这样的词,它还包括诸如陈述和进步等词。我理解这是因为我使用了LIKE运算符。
任何解决方案都将不胜感激!
由于
UPDATE !!! 我能够使用SJR的建议遍历D列!
我仍然遇到匹配确切字词的问题。对于诸如进步和状态之类的单词,代码还包括诸如进展,进步和陈述之类的单词。有什么方法可以找到完美匹配吗?
答案 0 :(得分:-1)
我编写了一个Python代码,我觉得这可能有所帮助。
def string_contains_exact(column_a, split_keyword):
# I am an american merchant ofcourse
# american merchant
if not split_keyword:
return True
for index in range(len(column_a)):
if split_keyword[0] == column_a[index]:
if string_contains_exact(column_a[index+1:], split_keyword[1:]):
return True
return False
def subset_if_present(input_data, keyword_set):
result = []
for row_ind in range(len(input_data)):
column_a = input_data[row_ind][0].lower().split()
for keyword in keyword_set:
split_keyword = keyword.split()
if string_contains_exact(column_a, split_keyword):
result.append((input_data[row_ind], keyword))
return result
input_data = [
['I have sample', 'b0', 'c0'],
['I have discover card', 'b1', 'c1'],
['I am enterprise', 'b2', 'c2'],
['I have nothing', 'b3', 'c3'],
['I have everything', 'b4', 'c4'],
['Can I paywhere', 'b5', 'c5'],
['what airport', 'b6', 'c6'],
['who are you', 'b7', 'c7'],
['what are the names', 'b8', 'c8'],
['who is the manufacturer', 'b9', 'c9'],
['where is it most accepted', 'b10', 'c10'],
['what names', 'b11', 'c11'],
['is the network down', 'b12', 'c12'],
['what state are you from', 'b13', 'c13'],
['when is the statement available', 'b14', 'c14'],
['how good is the progress', 'b15', 'c15'],
['are you progressive', 'b16', 'c16']
]
keyword_set = ["discover", "american merchant", "enterprise", "sample",
"hardware", "progress", "payanywhere", "acquirers", "airport", "annual","certified", "manufacturers", "most accepted", "names", "networks", "state"]
import pprint
pprint.pprint(subset_if_present(input_data, keyword_set))
这将打印行以及每个行中出现的关键字。对不起,但我不知道VBA。希望这会有所帮助。