Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1883|回复: 1

江湖急救---资产负债表转置变换

[复制链接]
发表于 2015-10-25 12:41 | 显示全部楼层 |阅读模式
已知有一个总的文件夹,每个文件夹又有6个folder,每个folder有对应的BS表,其中每个BS表的列不完全一致,现在需要合并汇总成图片中的格式
捕获.PNG

financial statements.rar

8.23 KB, 下载次数: 0

 楼主| 发表于 2015-10-25 12:43 | 显示全部楼层
已经解决了部分问题,想用数组来实现,可惜效率太低,运行不出来结果。
  1. Sub 测试()
  2. '
  3. ' 测试 宏


  4. Dim fso, f, n&
  5. Dim m As Integer
  6. Dim iR, i, j, k, z, y As Integer
  7. Dim 科目, data, temp
  8. Dim FilePath, FilePath2, MyStr  As String

  9. Dim WB As Workbook

  10.   FilePath = ThisWorkbook.Path
  11.   FilePath = FilePath & IIf(Right(FilePath, 1) = "", "", "")
  12.    n = 1
  13.    Set fso = CreateObject("Scripting.FileSystemObject")

  14.      For Each floder1 In fso.GetFolder(FilePath).SubFolders

  15.           FilePath = FilePath & floder1.Name & IIf(Right(FilePath, 1) = "", "", "")
  16.          
  17.         For Each f In floder1.Files
  18.           MyStr = f.Name
  19.           With CreateObject("VBSCRIPT.REGEXP")
  20.               .Pattern = "balance statement+"
  21.               .Global = True
  22.               
  23.             If .TEST(MyStr) Then Cells(n, 2) = FilePath & MyStr: n = n + 1
  24.         
  25.             End With
  26.          
  27.         Next
  28.         '  Workbooks(FilePath & MyStr).Close
  29.          
  30.          
  31.   FilePath = ThisWorkbook.Path
  32.   FilePath = FilePath & IIf(Right(FilePath, 1) = "", "", "")


  33.     Next
  34.      
  35.      iR = Range("B1").End(xlDown).Row
  36.    ' Cells(2, 4) = iR
  37.    
  38.    
  39.   

  40.    
  41.      FilePath = Cells(1, 2)
  42.      Set WB = Workbooks.Open(FilePath)
  43.      '  y = WB.Sheets("sheet1").Sheets("balance statement_EBBI10").Cells(1, 1).End(xlDown).Row
  44.      '  ReDim 科目(1 To y)
  45.        科目 = WB.Sheets(1).Columns(1).Value
  46.     ' 科目 = WB.Columns(1).Value
  47.    
  48.        ReDim data(1 To UBound(科目), 1 To 2)
  49.       
  50.      data = WB.Sheets(1).Range(Cells(1, 1), Cells(UBound(科目), 2))
  51.    ' MsgBox 科目(14, 1)
  52.       
  53. '  MsgBox data(14, 2)


  54. 'Workbooks(FilePath).Close
  55.    '------------------------------------------------------------------------------------
  56.      Rem  ReDim Preserve arr(1 To UBound(科目), 1 To iR + 1)
  57.    Rem =============================================================
  58.      '  For z = 1 To UBound(科目) + 1
  59.      '   arr(z, 1) = WB.Sheets("balance statement_EBBI10").Cells(z, 1).Value
  60.       '  arr(z, 2) = WB.Sheets("balance statement_EBBI10").Cells(z, 2).Value
  61.       '  Next z
  62.    WB.Close
  63.    
  64.     ReDim Preserve data(1 To UBound(科目), 1 To iR * 2)
  65.     For i = 2 To iR
  66.    
  67.      FilePath = Cells(i, 2)
  68.      Set WB = Workbooks.Open(FilePath)
  69.      
  70.     '  ReDim Preserve data(1 To UBound(科目), 1 To i * 2)
  71.        ReDim temp(1 To UBound(科目), 1 To 2)
  72.       
  73.    temp = WB.Sheets(1).Range(Cells(1, 1), Cells(UBound(科目), 2))
  74.     For j = 1 To UBound(科目)
  75.        ' For k = 0 To UBound(科目)
  76.          ' If arr(k, 1) = data(j, 1) Then data(j, i + 1) = arr(k, 2)
  77.     ' End If
  78.     '  Next k
  79.     '  Next j
  80.    ' data(j, 2 * i - 1) = WB.Sheets(1).Cells(j, 1)
  81.    ' data(j, 2 * i) = WB.Sheets(1).Cells(j, 2)
  82.      data(, 2 * i - 1) = WB.Sheets(1).Columns(1)
  83.      data(, 2 * i) = WB.Sheets(1).Columns(2)
  84.     Next j
  85.      WB.Close
  86.   Next i
  87.    
  88. MsgBox data(1, 4)
  89. 'Range(Cells(1, 10), Cells(UBound(科目) + 1, 11 + iR)) = arr()
  90. Rem ===================================================================='-------------------------------------------------------------------------
  91. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-29 21:40 , Processed in 0.656025 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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