【发布时间】: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
对于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 万次循环。
-
是的,把所有东西都放在数组中,只有在完成后才写回工作表。