How to group excel items based on custom rules?

后端 未结 3 1871
感动是毒
感动是毒 2021-01-19 16:39

I have a set of data (webmaster tools search queries) which is in excel with the following header:

Query | Impressions | Clicks | Date

Sample go

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 17:06

    And for a 3rd option, you could use a custom formula.

    I created a table just for categories on a separate sheet, then inserted the following code in a standard module.

    Option Explicit
    
    Function CategoryLookup(s_Query As String, Keyword_tbl As Range)
        Dim rngKeywords As Range
        Dim s_foundCategory As String
        Dim b_CategoryExists As Boolean
        b_CategoryExists = False
    
        For Each rngKeywords In Keyword_tbl
            If InStr(s_Query, rngKeywords.Value) <> 0 Then
                s_foundCategory = rngKeywords.Offset(0, 1).Value
                b_CategoryExists = True
                Exit For
            End If
        Next rngKeywords
    
        If b_CategoryExists = True Then
            CategoryLookup = s_foundCategory
        Else
            CategoryLookup = "Other"
        End If
    End Function
    

    Then in D2 (your category column) insert the following formula (which can then be dragged down)

    =CategoryLookup(A2,categories!$A$2:$A$5)
    

提交回复
热议问题