|
学不完用不尽 发表于 2013-6-30 22:00
套用到其他表中,科目字段中出现好多F项,怎么解决呢?
出现F1、F2……等无效字段名?加个判断:- Sub ADO加数组加字典()
- '引用Microsoft Scripting Runtime
- Dim cnn As New ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim rst As ADODB.Recordset
- Dim SQL$, arr_Field
- Dim p$, f$, arr, brr(1 To 65530, 1 To 6)
- Dim i&, j, v&, n&, r, d$, m$, y$, sh As Worksheet
- p = ThisWorkbook.Path & ""
- f = Dir(p & "*.xls")
- Application.ScreenUpdating = False
- Do While f <> ""
- If f <> ThisWorkbook.Name Then
- Set cnn = New ADODB.Connection
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;imex=1';Data Source=" & p & f
- Set rs = cnn.OpenSchema(adSchemaTables)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- shn = rs("TABLE_NAME").Value
- If Right(shn, 1) = "$" Then
- Set rst = cnn.Execute("[" & shn & "]")
- If rst.Fields(0).Name = "单位" Then
- y = Left$(f, 4)
- Set rst = New ADODB.Recordset
- SQL = "select * from [" & shn & "] where not 单位 is null"
- rst.Open SQL, cnn, 1, 3
- With rst
- ReDim arr_Field(1 To .Fields.Count - 1)
- For j = 1 To .Fields.Count - 1
- arr_Field(j) = .Fields(j).Name
- Next
- For i = 1 To .RecordCount
- w = .Fields(0).Value
- m = .Fields(1).Value
- d = .Fields(2).Value
- For j = 3 To .Fields.Count - 1
- If Left$(arr_Field(j), 1) <> "F" Then
- v = v + 1
- brr(v, 1) = w
- brr(v, 2) = y
- brr(v, 3) = m
- brr(v, 4) = d
- brr(v, 5) = arr_Field(j)
- brr(v, 6) = .Fields(j).Value
- End If
- Next
- .MoveNext
- Next
- End With
- End If
- End If
- End If
- rs.MoveNext
- Loop
- End If
- f = Dir
- Loop
- Cells.ClearContents
- [a1:f1] = Array("单位", "年", "月份", "店别", "科目", "数据")
- [a2].Resize(v, 6) = brr
- rs.Close
- Set rs = Nothing
- rst.Close
- Set rst = Nothing
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
评分
-
3
查看全部评分
-
|