|
其他都没动,指示这条语句的指向文件改为另外一个excel表,点运行只能导出标题行,求各位大神帮助,谢谢。
PathStr = "D:\Download\试验用电缆册\64000电缆册汇总.xlsm"
Sub Test4() '电缆总表排序
Dim Conn As Object, Rst As Object
Dim strConn As String, strSQL As String
Dim I As Integer, PathStr As String
Set Conn = CreateObject("ADODB.Connection") '启用连接对象
Set Rst = CreateObject("ADODB.Recordset") '查询出数据,放在记录集对象
PathStr = "D:\Download\试验用电缆册\64000电缆册汇总.xlsm" '设置工作簿的完整路径和名称 ThisWorkBook.FullName"
Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
Case Is <= 11
strConn = "Provider=Microsoft.Jet.01edb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
Case Is >= 12
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=Yes"";"""
End Select
'设置SQL查询语句
strSQL = "select * from [sheet1$] order by 起始区域 ASC, 终止区域 ASC"
Conn.Open strConn '打开数据库链接
Set Rst = Conn.Execute(strSQL) '执行查询,并将结果输出到记录集对象
With Sheet2
.Cells.Clear
For I = 0 To Rst.Fields.Count - 1 '填写标题
.Cells(1, I + 1) = Rst.Fields(I).Name
Next I
.Range("A2").CopyFromRecordset Rst
.Cells.EntireColumn.AutoFit '自动调整列宽
.Cells.EntireColumn.AutoFit '自动调整列宽
End With
Rst.Close '关闭数据库连接
Conn.Close
Set Conn = Nothing
Set Rst = Nothing
End Sub
|
|