|
本帖最后由 duquancai 于 2016-10-10 20:09 编辑
请下载附件测试》》》》》》》》》》》- Sub SQL加数组加字典()
- Dim cx As New 类1, sql1$, sql2$, sqlz$, sqll$, p$, f$, pa$, b As Boolean
- Dim d As Object, arr1(1 To 10000, 1 To 8), arr2(1 To 10000, 1 To 10), i&, n&, j&, k&, m&
- Set d = CreateObject("Scripting.Dictionary")
- p = ThisWorkbook.Path & "": pa = ThisWorkbook.FullName
- f = Dir(p & "*.xls*")
- Do While f <> ""
- If p & f = pa Then GoTo 100
- If Not b Then
- sql1 = "select * from [" & p & f & "].[资产负债表$a4:h]"
- sql2 = "select * from [" & p & f & "].[利润表$a4:j]"
- b = True
- Else
- sql1 = sql1 & " union all select * from [" & p & f & "].[资产负债表$a4:h]"
- sql2 = sql2 & " union all select * from [" & p & f & "].[利润表$a4:j]"
- End If
- 100 f = Dir
- Loop
- arr = cx.筛选结果(pa, sql1): brr = cx.筛选结果(pa, sql2)
- For i = 0 To UBound(arr, 2)
- If Not d.exists(arr(0, i)) Then
- n = n + 1
- d(arr(0, i)) = n
- For j = 1 To 8
- arr1(n, j) = arr(j - 1, i)
- Next
- Else
- m = d(arr(0, i))
- arr1(m, 3) = arr1(m, 3) + arr(2, i)
- arr1(m, 4) = arr1(m, 4) + arr(3, i)
- arr1(m, 7) = arr1(m, 7) + arr(6, i)
- arr1(m, 8) = arr1(m, 8) + arr(7, i)
- End If
- Next
- Sheet1.Cells.ClearContents: Sheet2.Cells.ClearContents
- Sheet1.Range("c:d,g:h").NumberFormat = "_ * #,##0.00_ ;_ * -#,##0.00_ ;_ * ""-""_ ;_ @_ "
- Sheet1.Range("a1:h1") = Array("资 产", "行次", " 期末余额", "年初余额", "负债和所有者(或股东权益)", "行次", "期末余额", "年初余额")
- Sheet1.Range("a2").Resize(10000, 8) = arr1
- d.RemoveAll: n = 0: m = 0
- For i = 0 To UBound(brr, 2)
- If Not d.exists(brr(0, i)) Then
- n = n + 1
- d(brr(0, i)) = n
- For j = 1 To 10
- arr2(n, j) = brr(j - 1, i)
- Next
- Else
- m = d(brr(0, i))
- For x = 3 To 5
- arr2(m, x) = arr2(m, x) + brr(x - 1, i)
- Next
- For x = 8 To 10
- arr2(m, x) = arr2(m, x) + brr(x - 1, i)
- Next
- End If
- Next
- Sheet2.Range("c:e,h:j").NumberFormat = "_ * #,##0.00_ ;_ * -#,##0.00_ ;_ * ""-""_ ;_ @_ "
- Sheet2.Range("a1:j1") = Array("项 目", "行次", "本期金额", "本年累计", "上年同期数", "项 目", "行次", "本期金额", "本年累计", "上年同期数")
- Sheet2.Range("a2").Resize(10000, 10) = arr2
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|