【问题标题】:Filtering themes based on words in a cell根据单元格中的单词过滤主题
【发布时间】:2021-03-02 20:00:25
【问题描述】:

我正在尝试根据 A:A 列中的单词将 C:C 列中的特定过滤器/主题分配给每一行。

例如A栏内容“交易信号是什么样的?”
我想在 C 列中通过名称“交易信号”来分配该特定行。

它没有正确过滤。它在所有行的所有 C 单元格中粘贴“未捕获”。

Sub EmailThemes()
Dim SearchRange As Range
Dim InsertRng As Range
Dim Cell As Range

Range("C8:C10000").Clear

Set SearchRange = Worksheets("Data").Range("A7:A1500")
Set InsertRng = Worksheets("Data").Range("C7:C1500")

For Each Cell In SearchRange
    If Cell.Value Like "*Chart*" = True Then
        InsertRng.Value = "Charts"
        
    ElseIf Cell.Value Like "*Investor*" = True Then
        InsertRng.Value = "Investor"
        
    ElseIf Cell.Value Like "*Trade Signals*" = True Then
        InsertRng.Value = "Trade Signals"
    
    ElseIf Cell.Value Like "*Account*" = True Then
        InsertRng.Value = "Account"
    
    ElseIf Cell.Value Like "*Watchlist*" & "*Trade*" & "*Order*" = True Then
        InsertRng.Value = "Trading"
    
    Else
        InsertRng.Value = "Not Captured"
    
    End If
    
Next Cell

End Sub

【问题讨论】:

  • 请同时添加一些带有所需输出的示例输入,以帮助我们更好地了解您的问题
  • 例如我有一行内容如下:A Column - Subject: FW: New columns added to the Trade blogger | B 栏 - 日期:2020 年 3 月 11 日 | C 列 - 主题:未捕获 |在这里,宏应该会发现主题包含“交易”一词并分配主题“交易”而不是主题“未捕获”。

标签: excel vba for-loop filtering


【解决方案1】:

您遍历SearchRng,但不会更新InsertRng 中的相应单元格,而是每一步都会覆盖整个InsertRng。

最后,您将始终拥有不符合任何条件的空单元格,您只会在各处得到“未捕获”。

调试代码总是一个好主意,这样您就可以看到运行每一行代码后会发生什么。

你可以这样做:

Intersect(Cell.EntireRow,InsertRng).Value = "..."

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 2012-07-03
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多