2014-02-24 133 views
3

我有一组数据(网站管理员工具搜索查询),其是在Excel具有以下标题:如何根据自定义规则对excel项进行分组?

Query | Impressions | Clicks | Date

样品谷歌电子表格here

我想在名为Category的额外增加一个字段,并根据会针对搜索字符串A列自定义规则分类的所有查询 例如:

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

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

=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"))) 

您能否提供这样做的更好的方法,我可以在一个表中的规则的格式如下:

Query_contains | Category_is

其中Query_contains将需要在要匹配的字符串列A从最初的表格和Category将是需要填写到列D的值。

+0

'Vlookup'应该做的到底是什么,你正在试图做的.. 。第一列将是你期望在A列中的值,第二列将是类别,然后你只需要查看该2列表...让我知道如果这没有意义.... –

+1

@JohnBus​​tos在示例电子表格中查看数据,一个简单的vlookup将不起作用。 – guitarthrower

+0

对不起,感谢@guitarthrower,我现在下载了工作表...现在发布解决方案... –

回答

8

好的,我改变了你片有点....

假设所有的数据是单元格A1:C9,那么您在单元格F1中加入如下表有:G5现在

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

,在D2单元格,把下面的公式:

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

而且输入为数组公式意义,一旦你进入它,打CTRL + SHIFT + ENTER

然后,您可以将公式从单元格D2向下拖动,它应该给您所需的结果(当然,您可以根据需要增加列F & G中的列表)。

希望这样做的窍门!

+0

+1好回答John! – guitarthrower

+0

您的答案获胜! –

+0

谢谢你们两位! - 这真的只取决于你是否想要VBA或公式......你的解决方案更有效率,但需要VBA ......只取决于你要寻找哪种解决方案。 –

3

这个小宏假设您的数据在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 
1

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

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

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) 
相关问题