|
大家好,我在学习用ADO法汇总同目录下多个文件时候,遇到如下问题:
用下面的语句汇总得到的结果会有部分单元格内容丢失,
不知道是什么原因?希望能得到大家的帮助!
图片中黄色部分是丢失了单元格内容!
图片中蓝色部分是“数值”内容被转换成文本!
Sub ADO()
Application.ScreenUpdating = False
Application.EnableEvents = False
Mypath = ThisWorkbook.Path & "\"
MyFile = Dir(Mypath & "*.xls")
Dim cnn As Object
Do While MyFile <> ""
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source=" & Mypath & MyFile
Set rs = cnn.openschema(20)
Do Until rs.EOF
If rs.Fields("TABLE_TYPE") = "TABLE" Then
sh = Replace(rs("TABLE_NAME").Value, "'", "")
If Right(sh, 1) = "$" Then
SQL = "select * from [" & sh & "] where 1=2"
Set rst = cnn.Execute(SQL)
If rst.Fields(0).Name <> "F1" Then
On Error Resume Next
SQL = "select 日期,产品名称,数量 from [" & sh & "]"
Sheet2.Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
Exit Do
End If
End If
End If
rs.movenext
Loop
MyFile = Dir()
Loop
rst.Close
rs.Close
cnn.Close
Set rst = Nothing
Set rs = Nothing
Set cnn = Nothing
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
|
|