|
原帖由 aa7338267 于 2008-12-25 09:03 发表
能够讲一下流程吗?
Dim RG, WB$, SQL$, ST$, LJ As Object
Set LJ = CreateObject("adodb.connection") '(抄的)
WB = Dir(ThisWorkbook.Path & "\*.xls") '从本工作簿所在目录取Excel文件名
Do While WB <> "" '如取到文件进入循环
If WB <> ThisWorkbook.Name Then '取到文件不是本工作簿则进行处理
LJ.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.Path & "\" & WB '与取到的文件连接(抄的)
For i = 1 To 5 '处理01到05表
SQL = "select * from [" & "0" & i & "$]" '确定读入范围和表名(抄的)
With Sheets("01-05") '选择目标:01-05表
.[a40].CopyFromRecordset LJ.Execute(SQL) '按读入范围将数据拷贝过来,临时放到40行以下(抄的)
RG = .Range("g43:i68") '将要处理的数据存入数组
For j = 5 To 30 '从5行到30行
For k = 7 To 9 '从7列到9列
.Cells(j, k).Value = .Cells(j, k).Value + RG(j - 4, k - 6) '每个单元格的数值与数组对应值相加
Next k
Next j
End With
Next i
For i = 6 To 10 '处理06到10表,方法同上
SQL = "select * from [" & "0" & i & "$]"
If i = 10 Then SQL = "select * from [10$]" '对10特别处理,去掉前面的0
With Sheets("06-10")
.[a40].CopyFromRecordset LJ.Execute(SQL)
RG = .Range("g43:i68")
For j = 5 To 30
For k = 7 To 9
.Cells(j, k).Value = .Cells(j, k).Value + RG(j - 4, k - 6)
Next k
Next j
End With
Next i
For i = 11 To 15 '处理11到15表,方法同上
SQL = "select * from [" & i & "$]"
With Sheets("11-15")
.[a40].CopyFromRecordset LJ.Execute(SQL)
RG = .Range("g43:i68")
For j = 5 To 30
For k = 7 To 9
.Cells(j, k).Value = .Cells(j, k).Value + RG(j - 4, k - 6)
Next k
Next j
End With
Next i
For i = 16 To 20 '处理16到20表,方法同上
SQL = "select * from [" & i & "$]"
With Sheets("16-20")
.[a40].CopyFromRecordset LJ.Execute(SQL)
RG = .Range("g43:i68")
For j = 5 To 30
For k = 7 To 9
.Cells(j, k).Value = .Cells(j, k).Value + RG(j - 4, k - 6)
Next k
Next j
End With
Next i
For i = 21 To 24 '处理21到24表,方法同上
SQL = "select * from [" & i & "$]"
With Sheets("21-24")
.[a40].CopyFromRecordset LJ.Execute(SQL)
RG = .Range("g43:i68")
For j = 5 To 30
For k = 7 To 9
.Cells(j, k).Value = .Cells(j, k).Value + RG(j - 4, k - 6)
Next k
Next j
End With
Next i
For i = 25 To 31 '处理25到31表,方法同上
SQL = "select * from [" & i & "$]"
With Sheets("25-31")
.[a40].CopyFromRecordset LJ.Execute(SQL)
RG = .Range("g43:i68")
For j = 5 To 30
For k = 7 To 9
.Cells(j, k).Value = .Cells(j, k).Value + RG(j - 4, k - 6)
Next k
Next j
End With
Next i
LJ.Close '关闭连接
End If
WB = Dir '取下一个Excel文件名
Loop
Set LJ = Nothing |
|