如何根据自定义规则对excel项进行分组?

我有一组数据(网站pipe理员工具search查询),这是在以下标题的Excel中:

Query | Impressions | Clicks | Date

在这里示例谷歌电子表格。

我想添加一个名为Category的额外列,并根据自定义规则对所有查询进行Category ,这些规则将在列A上searchstring。例如:

if A2 contains the string 'laptop' then write 'laptop' on the category next to it

到目前为止,我已经尝试了一个公式来做到这一点,但我不确定这是最简单的方法。 另外,如果有很多分类规则,公式变得非常长,难以pipe理。

 =IF(ISNUMBER(SEARCH("laptop",A2)),"laptop", IF(ISNUMBER(SEARCH("notebook",A2)),"laptop", IF(ISNUMBER(SEARCH("iphone",A2)),"phone", IF(ISNUMBER(SEARCH("galaxy s",A2)),"phone", "other"))) 

你可以build议一个更好的方法来做到这一点,我可以在这种格式的一张表中规则:

Query_contains | Category_is

其中Query_contains将是从列表中的A列中需要匹配的string,而Category则是需要填入列D的值。

好的,我换了一下你的床单。

假设你所有的数据都在单元格A1:C9中,那么你在单元格F1:G5中有下面的表格

 Search_For: Category: laptop Laptop iphone Phone galaxy Phone notebook Laptop 

现在,在D2单元格中,input以下公式:

 =IFERROR(INDEX(G$2:G$5,MATCH(TRUE,ISNUMBER(SEARCH(F$2:F$5,A2)),0)),"other") 

以数组公式的formsinput它,一旦你input它,点击CTRL + SHIFT + ENTER

然后,您可以从公式D2下拉公式,它应该给你想要的结果(当然,你可以根据需要增加列F&G中的列表)。

希望这个伎俩!

这个小macros假定您的数据在Sheet1中,而您的规则在A和B列中的工作表规则中:

 Sub catagorize() Dim s1 As Worksheet, s2 As Worksheet Dim N1 As Long, N2 As Long Set s1 = Sheets("Sheet1") Set s2 = Sheets("rules") N1 = s1.Cells(Rows.Count, "A").End(xlUp).Row N2 = s2.Cells(Rows.Count, "A").End(xlUp).Row For i = 2 To N1 v = s1.Cells(i, 1).Value For j = 1 To N2 If InStr(1, v, s2.Cells(j, 1).Value) > 0 Then s1.Cells(i, "D").Value = s2.Cells(j, "B").Value End If Next j Next i End Sub 

而对于第三个选项,您可以使用自定义公式。

我为一个单独的工作表上的类别创build了一个表格,然后将下面的代码插入到一个标准模块中。

 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 

然后在D2 (您的类别列)中插入下面的公式(然后可以拖动)

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