【问题标题】:Excel - Categorize table by columnExcel - 按列对表格进行分类
【发布时间】:2018-04-10 04:43:23
【问题描述】:

我目前有这张桌子:

我想按最后一列对其进行分类,使其如下所示:

我认为这对于数据透视表或其他东西可能是可行的,但似乎并非如此。我也尝试过使用切片器,但这并没有达到预期的效果(只是隐藏和取消隐藏行)。这似乎是一件很常见且足够简单的事情,但我似乎无法弄清楚。

编辑: 我真的不想重新创建图像中的表格,因为表格无法正确排序或搜索(因为描述类别的“标题”行将被错误排序并出现在搜索中),我只想它的显示与图像类似。

表格数据

| Armor           | Cost    | AC | Strength Requirement | Stealth      | Weight | Class        |
|-----------------|---------|----|----------------------|--------------|--------|--------------|
| Padded          | 5 gp    | 11 | —                    | Disadvantage | 8 lb   | Light Armor  |
| Leather         | 10 gp   | 11 | —                    | —            | 10 lb  | Light Armor  |
| Studded leather | 45 gp   | 12 | —                    | —            | 13 lb  | Light Armor  |
| Hide            | 10 gp   | 12 | —                    | —            | 12 lb  | Medium Armor |
| Chain shirt     | 50 gp   | 13 | —                    | —            | 20 lb  | Medium Armor |
| Scale mail      | 50 gp   | 14 | —                    | Disadvantage | 45 lb  | Medium Armor |
| Breastplate     | 400 gp  | 14 | —                    | —            | 20 lb  | Medium Armor |
| Half plate      | 750 gp  | 15 | —                    | Disadvantage | 40 lb  | Medium Armor |
| Ring mail       | 30 gp   | 14 | —                    | Disadvantage | 40 lb  | Heavy Armor  |
| Chain mail      | 75 gp   | 16 | 13                   | Disadvantage | 55 lb  | Heavy Armor  |
| Splint          | 200 gp  | 17 | 15                   | Disadvantage | 60 lb  | Heavy Armor  |
| Plate           | 1500 gp | 18 | 15                   | Disadvantage | 65 lb  | Heavy Armor  |
| Shield          | 10 gp   | +2 | —                    | —            | 6 lb   | Shield       |

【问题讨论】:

  • 我很困惑:你说你有“一张桌子”。这真的是整个列表吗?为什么需要编程?此外,似乎有数据是不在第一个表中的第二个表。数据透视表绝对可以按最后一列对您的列表进行分组......但它不能组成不存在的值。
  • 请同时包含数据作为插入,而不是图像。您可以使用诸如table generator 之类的工具来格式化表格数据,然后将其插入代码标签之间(Ctrl + K)
  • 你的意思是可以按最后一列搜索吗?
  • @QHarr 是的,我的意思是,在帖子中修复它。
  • @ashleedawg 1) 是的,那是一张桌子。 2)不需要编程(如果您知道如何使用数据透视表进行操作,请告诉)。 3) 我完全不知道你在说什么丢失的数据,这些表在功能上是相同的,只是看起来不同。

标签: excel


【解决方案1】:

您可以按如下方式创建自己的查找:

Option Explicit
Public outputRow   As Long
'VBE > Tools > references > tick MS HTML Object Library, MS XML

Public Sub Main()
    outputRow = 0
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Output")   ''change as appropriate
    ws.Cells.ClearContents
    GetTables ws
    AddKeys ws, 1
    ws.Cells.Columns.AutoFit
    ws.Columns("A:G").NumberFormat = "@"
End Sub

Public Sub GetTables(ByVal ws As Worksheet)
    Dim http As New XMLHTTP60, html As New HTMLDocument, arr() As Variant 'XMLHTTP60     This will vary according to your Excel version

    arr = Array("Currency", "Armor", "Selling Treasure", "Armor", "Weapons", _
                "Adventuring Gear", "Tools", "Mounts and Vehicles", "Trade Goods", "Expenses")

    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        DoEvents
        With http
            .Open "GET", ConstructURL(LCase$(arr(i))), False
            .send
            html.body.innerHTML = .responseText
        End With

        PrintTables html, ws
    Next i
End Sub

Public Sub PrintTables(ByVal html As HTMLDocument, ByVal ws As Worksheet)

    Dim rng As Range, tbl As HTMLTable, currentRow As Object, currentColumn As Object, i As Long, counter As Long

    For Each tbl In html.getElementsByTagName("Table")
        counter = counter + 1
        outputRow = outputRow + 1
        Set rng = ws.Range("B" & outputRow)
        rng.Offset(, -1) = "Table " & counter

        For Each currentRow In tbl.Rows
            For Each currentColumn In currentRow.Cells
                rng.Value = currentColumn.outerText
                Set rng = rng.Offset(, 1)
                i = i + 1
            Next currentColumn
            outputRow = outputRow + 1
            Set rng = rng.Offset(1, -i)
            i = 0
        Next currentRow
    Next tbl
End Sub

Public Function ConstructURL(ByVal item As String) As String
    ConstructURL = "https://dnd5e.info/equipment/" & item
End Function

Public Sub AddKeys(ByVal ws As Worksheet, Optional ByVal targetColumn As Long = 1)
    Dim loopColumn As Range, rng As Range
    Set loopColumn = ws.UsedRange.Columns(targetColumn)
    Dim cat As String

    For Each rng In loopColumn.Cells
        If InStr(1, rng.Text, "Table") > 0 Then
            cat = rng.Offset(, 1)
        End If
        If Not IsEmpty(rng.Offset(, 1)) And Not IsEmpty(rng.Offset(, 2)) Then
            If IsEmpty(rng) And Not IsEmpty(rng.Offset(, 2)) Then
                rng = cat & rng.Offset(, 1)
            End If
            If IsEmpty(rng) And IsEmpty(rng.Offset(, 2)) Then
                rng = cat & rng.Offset(, 1)
            End If
        End If
    Next rng
End Sub

输出:

注意 A 列有一个唯一键,您可以使用它来查找项目。但是,您需要知道您对哪一列感兴趣,尽管您可以再次匹配列标题。您可以对其进行整理,但已经适合对给定项目进行唯一查找。

【讨论】:

  • 对此有何反馈?
猜你喜欢
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 2014-11-06
  • 2019-07-19
  • 1970-01-01
相关资源
最近更新 更多