前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >常用功能加载宏——一维表转二维表

常用功能加载宏——一维表转二维表

作者头像
xyj
发布2020-07-28 14:38:12
8040
发布2020-07-28 14:38:12
举报
文章被收录于专栏:VBA 学习

实现了二维表格转换为一维表格,反过来的功能偶尔也是会用到的:

首先在customUI.xml中增加代码:

代码语言:javascript
复制
      <button id="rbbtnTarnsTable1To2" label="一维表转二维表" onAction="rbbtnTarnsTable1To2" supertip="将选择的多行3列表格转换为多行多列表格。"/>

回调函数:

代码语言:javascript
复制
Sub rbbtnTarnsTable1To2(control As IRibbonControl)
    Call MShtWk.TarnsTable1To2
End Sub

函数实现:

代码语言:javascript
复制
Sub TarnsTable1To2()
    Dim drow As Object
    Dim dcol As Object

    Set drow = VBA.CreateObject("Scripting.Dictionary")
    Set dcol = VBA.CreateObject("Scripting.Dictionary")

    Dim i As Long
    Dim arr() As Variant
    Dim rng As Range

    '确保选中的是单元格
    If TypeName(Selection) <> "Range" Then
        Exit Sub
    End If
    Set rng = Selection
    If rng.Columns.Count <> 3 Then
        MsgBox "只能处理3列数据,其中第3列必须是数字。"
        Exit Sub
    End If
    If rng.rows.Count < 2 Then
        MsgBox "数据至少要有2行。"
        Exit Sub
    End If
    arr = rng.Value
    
    Dim rngout As Range
    On Error Resume Next
    Set rngout = Application.InputBox("请选择输出的起始单元格。", Default:=rng.Range("A1").Offset(rng.rows.Count + 1, 0).Address, Type:=8)
    On Error GoTo 0
    If rngout Is Nothing Then Exit Sub
    Set rngout = rngout.Range("A1")
    
    '记录项目的行号、姓名的列号
    Dim strkey As String
    For i = 2 To UBound(arr)
        strkey = VBA.CStr(arr(i, 1))
        If Not drow.Exists(strkey) Then drow(strkey) = drow.Count + 1
        
        strkey = VBA.CStr(arr(i, 2))
        If Not dcol.Exists(strkey) Then dcol(strkey) = dcol.Count + 1
    Next
    
    Dim Result() As Variant
    ReDim Result(1 To drow.Count + 1, 1 To dcol.Count + 1) As Variant
    Result(1, 1) = "项目"
    
    Dim tmp
    tmp = drow.keys()
    '行
    For i = 0 To drow.Count - 1
        Result(i + 2, 1) = tmp(i)
    Next
    
    tmp = dcol.keys()
    '列
    For i = 0 To dcol.Count - 1
        Result(1, i + 2) = tmp(i)
    Next
    
    Dim pRow As Long, pcol As Long
    '数据
    For i = 2 To UBound(arr)
        pRow = drow(VBA.CStr(arr(i, 1))) + 1
        pcol = dcol(VBA.CStr(arr(i, 2))) + 1
        
        Result(pRow, pcol) = Result(pRow, pcol) + VBA.Val(arr(i, 3))
    Next
    rngout.Resize(drow.Count + 1, dcol.Count + 1).Value = Result
    
    Set drow = Nothing
    Set dcol = Nothing
End Sub
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-07-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 VBA 学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档