|
两帖合供参考.zip
(65.17 KB, 下载次数: 12)
- Sub test1() '纯练习,结合上帖,来个一步到位,仅供测试参考
-
- Dim s As String, strPath As String, strFile As String
- Dim Conn As Object, rs As Object, dict(1) As Object
- Dim data, results(), wks As Worksheet, ran As Range, target As Range, item_
- Dim strConn As String, strSQL As String, subSQL() As String, strFields As String, strTable As String
- Dim i As Long, j As Long, x As Long, y As Long, cnt As Long, idx As Long
-
- DoApp False
-
- For Each wks In Worksheets
- If wks.Index > 1 Then wks.Delete
- Next
-
- For j = LBound(dict) To UBound(dict)
- Set dict(j) = CreateObject("Scripting.Dictionary")
- Next
- Set Conn = CreateObject("ADODB.Connection")
- 'Set rs = CreateObject("ADODB.Recordset")
-
- s = "Excel 12.0;IMEX=1;HDR=YES;Database="
- If Application.Version < 12 Or InStr(Application.Path, "WPS") > 0 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
-
- strPath = ThisWorkbook.Path & Application.PathSeparator
- strFile = Dir(strPath & "*.xls*")
- While Len(strFile)
- If strPath & strFile <> ThisWorkbook.FullName Then
- cnt = cnt + 1
- If cnt = 1 Then
- With Workbooks.Open(strPath & strFile, 0)
- For Each wks In .Worksheets
- wks.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
- Next
- .Close False
- End With
- End If
- If Conn.State <> 1 Then Conn.Open strConn & strPath & strFile
- strSQL = strSQL & " UNION ALL SELECT [.strFields.] FROM [" & s & strPath & strFile & "].[[.strTable.]]"
- If cnt = 49 Then
- idx = idx + 1
- ReDim Preserve subSQL(1 To idx)
- subSQL(idx) = Mid(strSQL, 12)
- strSQL = vbNullString
- cnt = 0
- End If
- End If
- strFile = Dir
- Wend
- If cnt > 0 Then
- idx = idx + 1
- ReDim Preserve subSQL(1 To idx)
- subSQL(idx) = Mid(strSQL, 12)
- cnt = 0
- End If
-
- For Each wks In Worksheets
- With wks
- If .Index > 1 Then
- y = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row '.Cells(.Rows.Count, target.Column).End(xlUp).Row
- x = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column ' target.End(xlToRight).Column
- 'Set Target = .Range("A4")
- Set target = .UsedRange.Find("*编制单位*", , xlValues, , xlByRows, xlPrevious).Offset(1)
- Set ran = .Range(target, .Cells(y, x))
-
- For Each item_ In ran '若有公式 记录公式
- If item_.HasFormula Then dict(1).Add item_.Address(0, 0), item_.FormulaR1C1
- Next
-
- data = ran.Value
- ReDim results(1 To UBound(data) - 1, 1 To UBound(data, 2) - 1)
- For i = 2 To UBound(data)
- If Len(data(i, 1)) Then s = data(i, 1) Else s = "NULL"
- dict(0).Add s & "|" & CStr(i - 2), i - 1
- Next
- y = UBound(data) - 1
-
- strTable = .Name & "$" & ran.Address(0, 0)
- strFields = "`" & data(1, 1) & "`"
- For j = 2 To UBound(data, 2)
- If data(1, j) Like "*行*次*" Then
- s = ",NULL"
- For i = 1 To UBound(results)
- results(i, j - 1) = data(i + 1, j)
- Next
- Else
- s = ",`" & data(1, j) & "`"
- End If
- strFields = strFields & s
- Next
-
- For idx = LBound(subSQL) To UBound(subSQL)
- strSQL = Replace(Replace(subSQL(idx), "[.strFields.]", strFields), "[.strTable.]", strTable)
- Set rs = Conn.Execute(strSQL)
- data = rs.GetRows
- For x = 0 To UBound(data, 2)
- If Not IsNull(data(0, x)) Then s = data(0, x) & "|" & (x Mod y) Else s = "NULL" & "|" & (x Mod y)
- If dict(0).Exists(s) Then
- i = dict(0)(s)
- For j = 1 To UBound(data)
- If Not IsNull(data(j, x)) Then
- If Val(Replace(data(j, x), ",", "")) Then
- results(i, j) = Val(results(i, j)) + Val(Replace(data(j, x), ",", ""))
- Else
- If IsEmpty(results(i, j)) Then results(i, j) = data(j, x)
- End If
- End If
- Next
- End If
- Next
- Next
- target.Offset(1, 1).Resize(UBound(results), UBound(results, 2)) = results
-
- If dict(1).Count Then
- For Each item_ In dict(1).Keys '若有公式 回写公式
- .Range(item_).FormulaR1C1 = dict(1)(item_)
- Next
- End If
-
- cnt = cnt + 1
- Application.StatusBar = String(88, Chr(32)) & "完成 " & cnt & " / " & Worksheets.Count & " ,已处理: " & .Name
-
- End If
- End With
-
- For j = LBound(dict) To UBound(dict)
- dict(j).RemoveAll
- Next
- Next
-
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set target = Nothing
- Set ran = Nothing
- For j = LBound(dict) To UBound(dict)
- Set dict(j) = Nothing
- Next
-
- Worksheets(1).Activate
- DoApp
- 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
复制代码
|
|