|
楼主 |
发表于 2015-10-25 12:43
|
显示全部楼层
已经解决了部分问题,想用数组来实现,可惜效率太低,运行不出来结果。- Sub 测试()
- '
- ' 测试 宏
- Dim fso, f, n&
- Dim m As Integer
- Dim iR, i, j, k, z, y As Integer
- Dim 科目, data, temp
- Dim FilePath, FilePath2, MyStr As String
-
- Dim WB As Workbook
-
- FilePath = ThisWorkbook.Path
- FilePath = FilePath & IIf(Right(FilePath, 1) = "", "", "")
- n = 1
- Set fso = CreateObject("Scripting.FileSystemObject")
-
- For Each floder1 In fso.GetFolder(FilePath).SubFolders
- FilePath = FilePath & floder1.Name & IIf(Right(FilePath, 1) = "", "", "")
-
- For Each f In floder1.Files
- MyStr = f.Name
- With CreateObject("VBSCRIPT.REGEXP")
- .Pattern = "balance statement+"
- .Global = True
-
- If .TEST(MyStr) Then Cells(n, 2) = FilePath & MyStr: n = n + 1
-
- End With
-
- Next
- ' Workbooks(FilePath & MyStr).Close
-
-
- FilePath = ThisWorkbook.Path
- FilePath = FilePath & IIf(Right(FilePath, 1) = "", "", "")
- Next
-
- iR = Range("B1").End(xlDown).Row
- ' Cells(2, 4) = iR
-
-
-
-
-
- FilePath = Cells(1, 2)
- Set WB = Workbooks.Open(FilePath)
- ' y = WB.Sheets("sheet1").Sheets("balance statement_EBBI10").Cells(1, 1).End(xlDown).Row
- ' ReDim 科目(1 To y)
- 科目 = WB.Sheets(1).Columns(1).Value
- ' 科目 = WB.Columns(1).Value
-
- ReDim data(1 To UBound(科目), 1 To 2)
-
- data = WB.Sheets(1).Range(Cells(1, 1), Cells(UBound(科目), 2))
- ' MsgBox 科目(14, 1)
-
- ' MsgBox data(14, 2)
- 'Workbooks(FilePath).Close
- '------------------------------------------------------------------------------------
- Rem ReDim Preserve arr(1 To UBound(科目), 1 To iR + 1)
- Rem =============================================================
- ' For z = 1 To UBound(科目) + 1
- ' arr(z, 1) = WB.Sheets("balance statement_EBBI10").Cells(z, 1).Value
- ' arr(z, 2) = WB.Sheets("balance statement_EBBI10").Cells(z, 2).Value
- ' Next z
- WB.Close
-
- ReDim Preserve data(1 To UBound(科目), 1 To iR * 2)
- For i = 2 To iR
-
- FilePath = Cells(i, 2)
- Set WB = Workbooks.Open(FilePath)
-
- ' ReDim Preserve data(1 To UBound(科目), 1 To i * 2)
- ReDim temp(1 To UBound(科目), 1 To 2)
-
- temp = WB.Sheets(1).Range(Cells(1, 1), Cells(UBound(科目), 2))
- For j = 1 To UBound(科目)
- ' For k = 0 To UBound(科目)
- ' If arr(k, 1) = data(j, 1) Then data(j, i + 1) = arr(k, 2)
- ' End If
- ' Next k
- ' Next j
- ' data(j, 2 * i - 1) = WB.Sheets(1).Cells(j, 1)
- ' data(j, 2 * i) = WB.Sheets(1).Cells(j, 2)
- data(, 2 * i - 1) = WB.Sheets(1).Columns(1)
- data(, 2 * i) = WB.Sheets(1).Columns(2)
- Next j
- WB.Close
- Next i
-
- MsgBox data(1, 4)
- 'Range(Cells(1, 10), Cells(UBound(科目) + 1, 11 + iR)) = arr()
- Rem ===================================================================='-------------------------------------------------------------------------
- End Sub
复制代码 |
|