使用宏来排序VBA

时间:2016-07-12 12:22:15

标签: excel vba excel-vba

我正在将一些数据从txt文件导入excel

它看起来如何:

licence: MD2
approval: granted
id: 42FB1010C6B1ED82-0a292138.1
client: CH-W-7000361
username:asd

licence: CAI
approval: granted
id: 42FB1010C6B1ED82-0a292138.1
client: CH-W-7000361
username:qwe

因此它具有相同许可证但具有不同批准等的多个条目

我想要做的是:过滤数据并为其提供结构,使其看起来像:

有关我如何订购源数据的“规则”指南。只有许可证出现

  • 许可证
  • 批准(授予/未授予)
  • id / client / username

它应该如何:

licence: EDR
approval: granted
  client: pf-W-12365
approval: not granted
  client: QT-B-7000361
  client: C-S-7000361

我用数据透视表做了这个,但我想用宏

自动化它

我的代码 我做了这个

Sub Order()
Dim dicKey As String
Dim dicValues As String
Dim dic
Dim data

data = Range("A1:D100") 'load data into variable
    With CreatObject("scripting.dictionary")
        For i = 1 To UBound(data)
            If .Exists(data(i, 1)) Then 'test to see if the key exists
            Else
                dicKey = data(i, 1) 'set the Key
                dicValues = data(i, 2) 'set the value for data to be stored
                .Add dicKey, dicValues
            End If

            Next i
            For Each dic In .Keys
                If .Items(dic) <> "" Then
                Debug.Print (dic)
                'do stuff
                End If
            Next dic
        End With
End Sub

1 个答案:

答案 0 :(得分:0)

作为替代方案,我建议您使用 Power Query 加载文件。它是Microsoft提供的免费Add In,随Excel 2013和2016一起提供,可以免费下载到Excel 2010.

https://support.office.com/en-us/article/Introduction-to-Microsoft-Power-Query-for-Excel-6e92e2f4-2079-4e1f-bad5-89f6269cd605

该工具可让您轻松设置自动查询以清理和整形数据。您可以拆分文本值,排序,过滤等。

它更加用户友好,并产生更可维护的结果。