|
楼主 |
发表于 2024-4-17 09:20
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
老师编写的原代码如下:
Sub xmbd()
Dim Cat As Object, Cn As Object, ObjTable As Object
Dim Path$, FileName$, StrCn$, StrSQL$, Arr As Variant, Brr As Variant, StrCommand$
Set WshShell = CreateObject("WScript.Shell")
ReDim Arr(1 To 1000, 1 To 5)
Set Cat = CreateObject("ADOX.Catalog")
Set Cn = CreateObject("ADODB.Connection")
Path = ThisWorkbook.Path & "\"
FileName = Dir(Path & "*.xlsx")
Do While FileName <> ""
i = i + 1
StrCn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Path & FileName
Cn.Open StrCn
Cat.ActiveConnection = StrCn
For Each ObjTable In Cat.Tables
If Not ObjTable.Name Like "*FilterDatabase" And ObjTable.Type = "TABLE" And ObjTable.Name <> "报送单$" Then
StrSQL = StrSQL & " Union All Select * From [Excel 12.0;DataBase=" & Path & FileName & "].[" & Replace(ObjTable.Name, "'", "") & "F3:F] Where 页数>0"
End If
Next ObjTable
StrSQL = "Select Sum(页数) From (" & Mid(StrSQL, 12) & ")"
Brr = Cn.Execute(StrSQL).Getrows
Arr(i, 1) = Split(FileName)(0)
Arr(i, 2) = Split(Split(FileName)(1), ".xlsx")(0)
Arr(i, 3) = Brr(0, 0)
StrCommand = "Powershell [System.IO.Directory]::GetFiles('" & Path & Split(FileName, ".xlsx")(0) & "').Count"
Set WshShellExec = WshShell.Exec(StrCommand)
Arr(i, 4) = Split(WshShellExec.StdOut.ReadAll, Chr(13))(0)
Arr(i, 5) = Arr(i, 3) - Arr(i, 4)
StrSQL = ""
Cn.Close
FileName = Dir
Loop
Range("B2").Resize(UBound(Arr), UBound(Arr, 2)) = Arr
Range("A2:A65536").ClearContents
For j = 1 To Cells(Rows.Count, 2).End(xlUp).Row - 1
Cells(j + 1, 1) = j 'B列
Next
End Sub
|
|