实例14:不打开文件而引用单列、单行、某个单元格、横向单元格的和、纵向单元格的和: '引用一列,如A列 Sub onecolumn() Dim Sql$ Set Conn = CreateObject("Adodb.Connection") Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & ThisWorkbook.Path & "\1.xls" Sql = "select f1 from [sheet1$]" Cells.Clear [a1].CopyFromRecordset Conn.Execute(Sql) Conn.Close Set Conn = Nothing End Sub '引用一行,如第1行 Sub onerow() Dim Sql$ Set Conn = CreateObject("Adodb.Connection") Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & ThisWorkbook.Path & "\1.xls" Sql = "select * from [sheet1$a1:iv1]" Cells.Clear [a1].CopyFromRecordset Conn.Execute(Sql) Conn.Close Set Conn = Nothing End Sub '引用一个单元格,如 k1 单元格 Sub onecell() Dim Sql$ Set Conn = CreateObject("Adodb.Connection") Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & ThisWorkbook.Path & "\1.xls" Sql = "select * from [sheet1$k1:k1]" Cells.Clear [a1].CopyFromRecordset Conn.Execute(Sql) Conn.Close Set Conn = Nothing End Sub
'计算 A1+B1 Sub A1_Plus_b1() Dim Sql$ Set Conn = CreateObject("Adodb.Connection") Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & ThisWorkbook.Path & "\1.xls" Sql = "select f1+f2 from [sheet1$a1:b1]" Cells.Clear [a1].CopyFromRecordset Conn.Execute(Sql) Conn.Close Set Conn = Nothing End Sub
'计算 A1+A2 Sub sumcolumn() Dim Sql$ Set Conn = CreateObject("Adodb.Connection") Conn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & ThisWorkbook.Path & "\1.xls" Sql = "select sum(f1) from [sheet1$a1:a2]" Cells.Clear [a1].CopyFromRecordset Conn.Execute(Sql) Conn.Close Set Conn = Nothing End Sub
CzVYNsA7.rar
(12.07 KB, 下载次数: 313)
[此贴子已经被作者于2007-3-9 13:10:10编辑过] |