ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 156|回复: 4

.Transpose(.Transpose(Arr)数组转置,Dictionary如何转置?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-25 11:01 | 显示全部楼层 |阅读模式



  1. Sub lll()
  2.   Dim Arr(1)
  3.    Arr(0) = Array(1, 2, 3)
  4.    Arr(1) = Array(4, 5, 6)
  5.    With Application.WorksheetFunction
  6.        Sheet1.Cells(1, 1).Resize(2, 3) = .Transpose(.Transpose(Arr))
  7.    End With
  8.    Stop
  9. End Sub
复制代码



.Transpose(.Transpose(Arr)数组转置结果,如下。
image.png

*****************
二楼问的主题,字典如何转置??上图结果。

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-25 11:14 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助


.Transpose(.Transpose(Dict.Items))的应用?

  1. Sub ll2()
  2.   Dim Dict  As Dictionary
  3.       Set Dict = New Dictionary
  4.   Dim Arr(1)
  5.    Arr(0) = Array(1, 2, 3)
  6.    Arr(1) = Array(4, 5, 6)
  7.    For ii = 0 To 1
  8.        Dict("ii" & ii) = Arr(ii)
  9.    Next ii
  10.    ''
  11.    With Application.WorksheetFunction
  12.        Sheet1.Cells(6, 1).Resize(Dict.Count, 1) = .Transpose(Dict.Keys)
  13.        Sheet1.Cells(6, 3).Resize(Dict.Count, 3) = .Transpose(.Transpose(Dict.Items))
  14.       
  15.       
  16.        Sheet1.Cells(1, 1).Resize(2, 3) = .Transpose(.Transpose(Arr))
  17.    End With
  18.    Stop
  19. End Sub
复制代码


字典运行结果。
image.png

TA的精华主题

TA的得分主题

发表于 2024-5-25 13:41 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
keys或items可以当成一维数组进行转置。。。

TA的精华主题

TA的得分主题

发表于 2024-5-26 19:07 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
转置后数组元素个数可能会丢失
Keys 是唯一值  但是Items 可以重复
转置后 Items 变 Keys 元素可能会少

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-5-28 19:07 | 显示全部楼层

  1. Sub Test()
  2.     t = Time
  3.     Dim Dict0 As Dictionary
  4.     Set Dict0 = New Dictionary

  5.     Set Dict1 = New Dictionary
  6.     Set Dict2 = New Dictionary
  7.     Set FolderDict = New Dictionary
  8.     Dim Rng As Range, R1 As Range, R2 As Range
  9.         Set Rng = Selection
  10.     Dim Sht As Worksheet
  11.         
  12.         Sht.Cells.Clear
  13.     Dim Rr, Cc
  14.         Rr = 10
  15.         Cc = 4
  16.     Dim Fso As FileSystemObject
  17.         Set Fso = New FileSystemObject
  18.     Dim oFolder As Folder, oFile As File
  19.    
  20.     Set oFolder = Fso.GetFolder(ThisWorkbook.Path )
  21.     If oFolder.SubFolders.Count = 0 And oFolder.Files.Count > 0 Then
  22.         TraverseFolderFile oFolder
  23.     Else
  24.         TraverseSubFolders oFolder
  25.     End If
  26.     With Application.WorksheetFunction
  27.         Sht.Cells(Rr, 1).Resize(Dict1.Count, 1) = .Transpose(Dict1.Keys)
  28.         Sht.Cells(Rr, Cc + 15).Resize(Dict2.Count, 1) = .Transpose(Dict2.Keys)
  29.         Sht.Cells(Rr, Cc + 18).Resize(FolderDict.Count, 1) = .Transpose(FolderDict.Keys)
  30.         Sht.Cells(Rr, Cc + 19).Resize(FolderDict.Count, 1) = .Transpose(FolderDict.Items)
  31.     End With
  32.     Sht.Cells(1, 1) = "Not Repeat Files " & Dict1.Count
  33.     Sht.Cells(1, 2) = "Total Files " & Dict2.Count
  34.     Sht.Cells(1, 3) = "Total Folder " & FolderDict.Count
  35.    
  36.     ''Debug.Print
  37.     With Dict1
  38.          '''
  39.          For ii = 0 To Dict1.Count - 1
  40.               Set Dict0 = RepeatDict(.Keys(ii), .Items(ii), Dict2)
  41.               Debug.Print Dict0.Count
  42.               If Dict0.Count > 0 Then
  43.                   With Application.WorksheetFunction
  44.                       Sht.Cells(Rr + ii, Cc) = Dict0.Count
  45.                      
  46.                       Sht.Cells(Rr + ii, Cc + 1) = .Transpose(.Transpose(Dict0.Keys))
  47.                       Sht.Cells(Rr + ii, Cc + 1) = .Transpose(Dict0.Keys) '.Transpose(.Transpose(Dict0.Keys))
  48.                       Sht.Cells(Rr + ii, Cc + 1).Resize(, Dict0.Count) = .Transpose(Dict0.Keys) '.Transpose(.Transpose(Dict0.Keys))
  49.                      
  50.                   End With
  51.               End If
  52.          Next ii
  53.     End With
  54.     Sht.Cells(3, 1) = Format(Time - t, "h:mm:ss")
  55. End Sub
  56. Function RepeatDict(Str1, Str2, Dict As Dictionary)
  57.   Dim oDict As Dictionary
  58.       Set oDict = New Dictionary
  59.       For ii = 0 To Dict.Count - 1
  60.           'Debug.Print Str1, Dict.Items(ii), Str2, Dict.Keys(ii)
  61.           If Str1 = Dict.Items(ii) And Str2 <> Dict.Keys(ii) Then
  62.               oDict(Dict.Keys(ii)) = ""
  63.           End If
  64.       Next ii
  65.       'Debug.Print oDict.Count
  66.       Set RepeatDict = oDict
  67. End Function

  68. Sub TraverseSubFolders(oFolder As Folder)
  69.    
  70.     Dim SubFolder As Folder
  71.    
  72.     For Each SubFolder In oFolder.SubFolders
  73.         FolderDict(SubFolder.Name) = SubFolder.Path
  74.         TraverseFolderFile SubFolder
  75.         TraverseSubFolders SubFolder
  76.         
  77.     Next SubFolder
  78. End Sub

  79. Function TraverseFolderFile(oFolder As Folder)
  80.    Dim oFile As File
  81.      For Each oFile In oFolder.Files
  82.          'Debug.Print , oFile.Path
  83.          Dict1(oFile.Name) = oFile.Path
  84.          Dict2(oFile.Path) = oFile.Name
  85.      Next oFile
  86. End Function



复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-17 07:54 , Processed in 0.032113 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表