|
本帖最后由 xiangbaoan 于 2022-11-5 09:22 编辑
- <div class="blockcode"><blockquote>Sub test1()
- Dim s As String, p As String, f As String
- Dim vResult(), vTemp, Conn As Object, rs As Object, Dic As Object, Dict As Object
- Dim Sht As Worksheet, Ran As Range, Cel As Range, m As Long, n As Long, i As Long, j As Long, k
- Dim strConn As String, subSQL As String, SQL As String, sFields As String, sTable As String
-
- DoApp False
-
- Set Conn = CreateObject("ADODB.Connection")
- 'Set rs = CreateObject("ADODB.Recordset")
-
- s = "Excel 12.0;IMEX=1;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;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
-
- Conn.Open strConn & ThisWorkbook.FullName
-
- p = ThisWorkbook.Path & Application.PathSeparator
- f = Dir(p & "*.xls?")
-
- While Len(f)
- If ThisWorkbook.FullName <> p & f Then _
- subSQL = subSQL & " UNION ALL SELECT [-sFields-] FROM [" & s & p & f & "].[[-sTable-]]"
- f = Dir
- Wend
- subSQL = Mid(subSQL, 12)
-
- Set Dic = CreateObject("Scripting.Dictionary")
- Set Dict = CreateObject("Scripting.Dictionary")
-
- For Each Sht In Worksheets
- With Sht
- If .Index > 3 Then
- Set Cel = .Range("A3")
- i = .Cells(.Rows.Count, "A").End(xlUp).Row 'Cel.End(xlDown).Row
- j = Cel.End(xlToRight).Column
- Set Ran = .Range(Cel, .Cells(i, j))
- For Each k In Ran
- If k.HasFormula Then Dict.Add k.Address(0, 0), k.FormulaR1C1
- Next
- vTemp = Ran.Value
- ReDim vResult(1 To UBound(vTemp) - 1, 1 To UBound(vTemp, 2) - 1)
- For i = 2 To UBound(vTemp)
- If Len(vTemp(i, 1)) Then Dic.Add vTemp(i, 1) & "|" & CStr(i - 2), i - 1
- Next
- k = UBound(vTemp) - 1
- sTable = .Name & "$" & Ran.Address(0, 0)
- sFields = "[" & vTemp(1, 1) & "]"
- For j = 2 To UBound(vTemp, 2)
- sFields = sFields & ",[" & vTemp(1, j) & "]"
- Next
- SQL = Replace(Replace(subSQL, "[-sFields-]", sFields), "[-sTable-]", sTable)
- SQL = "SELECT * FROM (" & SQL & ")" ' ORDER BY " & vTemp(1, 1)
- 'rs.Open SQL, Conn, 1, 3
- Set rs = Conn.Execute(SQL)
- vTemp = rs.GetRows
- 'If rs.State = 1 Then rs.Close
- For n = 0 To UBound(vTemp, 2)
- If Not IsNull(vTemp(0, n)) Then
- If Dic.Exists(vTemp(0, n) & "|" & (n Mod k)) Then
- i = Dic(vTemp(0, n) & "|" & (n Mod k))
- For j = 1 To UBound(vTemp)
- If Not IsNull(vTemp(j, n)) Then
- If Val(vTemp(j, n)) Then
- vResult(i, j) = Val(vResult(i, j)) + Val(vTemp(j, n))
- Else
- If IsEmpty(vResult(i, j)) Then vResult(i, j) = vTemp(j, n)
- End If
- End If
- Next
- End If
- End If
- Next
- Cel.Offset(1, 1).Resize(UBound(vResult), UBound(vResult, 2)) = vResult
- Erase vResult
- If Dict.Count Then
- For Each k In Dict.Keys
- .Range(k).FormulaR1C1 = Dict(k)
- Next
- End If
- m = m + 1
- Application.StatusBar = Space(88) & "完成 " & m & " / " & Worksheets.Count - 3 & " ,已处理: " & .Name
- End If
- End With
- Dic.RemoveAll
- Dict.RemoveAll
- Next
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set Cel = Nothing
- Set Ran = Nothing
- Set Sht = Nothing
- Set Dic = Nothing
- Set Dict = Nothing
- DoApp True
- End Sub
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- .Calculation = -b * 30 - 4135
- If b Then .StatusBar = vbNullString: Beep
- End With
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|