【问题标题】:Finding string and copying cells查找字符串并复制单元格
【发布时间】:2018-07-23 02:15:23
【问题描述】:

数据

输出

(请参阅上面的数据和输出)

这是我的示例数据。我正在寻找的是每次在 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

Output

对于state、progress等词,也包括statement、progression等词。我知道这是因为我使用 LIKE 运算符。

任何解决方案将不胜感激!

谢谢

更新!!! 我能够使用 SJR 的建议遍历 D 列!

我仍然无法匹配确切的单词。对于进度和状态等词,代码还包括进度、渐进和陈述等词。有什么方法可以找到完美的匹配?

【问题讨论】:

  • 你需要星号If Range("A" & i).Value Like "*" & Range("D" & j) & "*"
  • 您需要单独循环遍历 D 列。根据 a 中的值测试每一个,直到找到匹配项。
  • 我有,使用 j = 2 到 10。我现在正在尝试 SJR 的建议,但 excel 只是冻结了。我将对上面的代码进行编辑以显示。
  • 你真的有90万行数据吗!?您正在执行 900 万次循环。
  • 是的,把所有东西都放在数组中,只有在完成后才写回工作表。

标签: string vba excel search


【解决方案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。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多