|
本帖最后由 baofa2 于 2024-4-18 20:56 编辑
- Sub test0() '更新一下,仅供测试,不适用于WPS
-
- Dim ar, br, Conn As Object, dic As Object, dict As Object
- Dim s As String, p As String, f As String
- Dim strConn As String, SQL As String
- Dim i As Long, j As Long, k As Long
-
- Application.ScreenUpdating = False
-
- Set dic = CreateObject("Scripting.Dictionary")
- Set dict = CreateObject("Scripting.Dictionary")
- Set Conn = CreateObject("ADODB.Connection")
-
- With Range(Range("B2").End(xlDown).Offset(, -1), Cells(2, Columns.Count).End(xlToLeft))
- .Offset(1, 4).ClearContents
- .Columns(3).Offset(1).ClearContents
- ar = .Value
- End With
- For i = 5 To UBound(ar, 2)
- s = Replace(ar(1, i), "年", "")
- If Not dic.Exists(s) Then dic.Add s, i
- Next
- For i = 2 To UBound(ar)
- s = ar(i, 4)
- If Not dic.Exists(s) Then dic.Add s, i
- Next
-
- s = "Excel 12.0;HDR=YES;Database="
- If Application.Version < 12 Then
- s = Replace(s, "12.0", "8.0")
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls*")
- SQL = "SELECT 单位名称,单位编号,FORMAT(`对应费款_所属期`,'0000-00') AS 期,`单位应_缴金额`*1 AS 额 FROM [" & s & p & "[.f]].[单位欠费信息查询$B5:L] WHERE LEN(单位编号)"
- Do
- If p & f <> ThisWorkbook.FullName Then
- dict.Add Replace(SQL, "[.f]", f), vbNullString
- If dict.Count = 40 Then
- br = Conn.Execute(Join(dict.Keys, " UNION ALL ")).GetRows()
- ProcessData ar, br, dic
- dict.RemoveAll
- End If
- End If
- f = Dir
- Loop While Len(f)
-
- If dict.Count Then
- br = Conn.Execute(Join(dict.Keys, " UNION ALL ")).GetRows()
- ProcessData ar, br, dic
- End If
-
- Dim total(1)
- ReDim sum_(5 To UBound(ar, 2)) As Double
- For k = 0 To UBound(total)
- total(k) = sum_
- Next
- For i = 2 To UBound(ar)
- For j = LBound(sum_) To UBound(sum_)
- For k = 0 To UBound(total)
- total(k)(j) = total(k)(j) + Val(ar(i, j))
- Next
- Next
- If InStr(ar(i, 2), "汇总") Then
- For j = LBound(sum_) To UBound(sum_)
- If total(0)(j) Then ar(i, j) = total(0)(j)
- total(0)(j) = 0
- Next
- End If
- If InStr(ar(i, 2), "总计") Then
- For j = LBound(sum_) To UBound(sum_)
- If total(1)(j) Then ar(i, j) = total(1)(j)
- Next
- End If
- Next
-
- With Range("A2")
- With .Resize(UBound(ar), UBound(ar, 2))
- .Columns(4).NumberFormatLocal = "@"
- .Value = ar
- End With
- End With
-
- Conn.Close
- Set Conn = Nothing
- Set dic = Nothing
- Set dict = Nothing
-
- Application.ScreenUpdating = True
- Beep
- End Sub
- Function ProcessData(ar, br, dic As Object)
- Dim cr, j As Long, s As String, posRow As Long, posCol As Long
- For j = 0 To UBound(br, 2)
- If Not IsNull(br(3, j)) Then
- posCol = 0
- s = Trim(br(1, j))
- If dic.Exists(s) Then
- posRow = dic(s)
- If ar(posRow, 3) = "" Then ar(posRow, 3) = br(0, j)
- cr = Split(br(2, j), "-")
- If dic.Exists(cr(0)) Then posCol = dic(cr(0)) Else posCol = dic(cr(0) & Array("上", "下")(1 + (Val(cr(1)) < 7)))
- If posCol Then
- ar(posRow, posCol) = ar(posRow, posCol) + br(3, j)
- ar(posRow, UBound(ar, 2)) = ar(posRow, UBound(ar, 2)) + br(3, j)
- End If
- End If
- End If
- Next
- End Function
复制代码
|
|