|
tokem 发表于 2014-3-27 07:07
谢谢高手的耐心解答!
程序中有很多不懂的地方,主要是以下几个:
1)07-10行的作用是什么?arr变量的 ...
程序使用了ADO、字典和数组知识,建议学习一下这些知识:- Sub Macro1()
- Dim cnn As Object, SQL$, MyPath$, MyFile$, n%, arr, brr(1 To 60000, 2 To 8), i&, c, d As Object, wb As Workbook
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set wb = Workbooks.Open(ThisWorkbook.Path & "\Performance.xls")
- Set d = CreateObject("scripting.dictionary") '创建字典对象
- arr = wb.Sheets("Template").[a18].CurrentRegion 'Template表a18的当前区域值写入数组
- For i = 3 To UBound(arr, 2) Step 2 '逐列,步长2
- If Len(arr(1, i)) Then d(arr(1, i)) = i '如果有值则添加到字典键值,列号添加到字典条目
- Next
- MyPath = ThisWorkbook.Path & "\Wizard"
- MyFile = Dir(MyPath & "*.xls")
- Do While MyFile <> ""
- If MyFile <> ThisWorkbook.Name Then
- n = n + 1
- brr(n, 2) = Replace(MyFile, ".xls", "")
- If n = 1 Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='excel 8.0;hdr=no';Data Source=" & MyPath & MyFile
- SQL = "select * from [report$a43:o47]"
- Else
- SQL = "select * from [Excel 8.0;hdr=no;Database=" & MyPath & MyFile & ";].[report$a43:o47]"
- End If
- arr = cnn.Execute(SQL).GetRows
- brr(n, 2) = Replace(MyFile, ".xls", "")
- For i = 1 To UBound(arr) '逐列
- If Not IsNull(arr(i, 0)) Then '如果该值不为空
- c = d(arr(i, 0)) '字典键值赋值给变量c
- If c <> "" Then '如果c不是空串,c就是字典储存的列号,
- brr(n, c) = arr(i, 1) '该列号赋值
- brr(n, c + 1) = arr(i, 2) '该列号右边一列赋值
- End If
- End If
- Next
- End If
- MyFile = Dir()
- Loop
- wb.Sheets("Template").Range("B21:H45").ClearContents
- wb.Sheets("Template").[b21].Resize(n, 7) = brr
- wb.Close True
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- MsgBox "ok"
- End Sub
复制代码 |
|