|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
syub97 发表于 2012-3-15 13:28
赵老师,都怪我没有说清楚,我给的附件的表头是确定的,这种情况下确实可以直接用表头去读取数据。
但是 ... - Sub Macro4() '假设第一个字段是“IMPA”,程序没有判断
- Dim cnn As Object, rs As Object, SQL$, arr(), i%
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.path & "\IMPA船舶物料指南(第4版)电子版.xls"
- Set rs = cnn.Execute("[英文$]")
- ReDim arr(1 To rs.Fields.Count - 1)
- For i = 1 To rs.Fields.Count - 1
- arr(i) = rs.Fields(i).Name
- Next
- ActiveSheet.UsedRange.Offset(, 1).ClearContents
- [b1].Resize(, i - 1) = arr
- SQL = "select a.IMPA" & ",b." & Join(arr, ",b.") & " from [Excel 8.0;Database=" & ThisWorkbook.FullName & "].[Sheet1$a:a] a left join [英文$] b on a.IMPA = b.IMPA"
- [a2].CopyFromRecordset cnn.Execute(SQL)
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|