|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
代码是从Home下的,可以正常运行,问代码中的两个小问题
RST.Open "select * from [Sheet1$A1:H12]", CNN, 1, 3
其中的[Sheet1$A1:H12] 工作表怎么用变量表示,有网友给的建议[" & Sheets(i).Name & "$A1:H12],但不能完全正常运行
睡了,希望明天起来有个好结果
[Sheet1$A1:H12]其中工作表用变量是怎么写的,
行号及列号的变量向下面这样写是可以,有没有更简单的
[Sheet1$" & Chr(97 + j) & (i + 1) & ":" & Chr(97 + j) & (i + 1) & "]
还有这个地址变量又是怎么写的呢
"data source=D:\Book1.xls"
如果我是要复制到本工作簿,地址又是怎么写的
代码如下
Sub bb()
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim arr() As Variant
Dim iRow As Integer
Dim iCol As Integer
With CNN
.ConnectionString = "provider=microsoft.jet.oledb.4.0;" & _
"extended properties='Excel 8.0;hdr=no';" & _
"data source=D:\Book1.xls"
.Open
End With
RST.Open "select * from [Sheet1$A1:H12]", CNN, 1, 3
iRow = RST.RecordCount
iCol = RST.Fields.Count
ReDim arr(1 To iRow, 1 To iCol) As Variant
For i = 1 To iRow
For j = 1 To iCol
arr(i, j) = RST.Fields(j - 1).Value
Next j
RST.MoveNext
Next i
'RST.Update
RST.Close
CNN.Close
With CNN
.ConnectionString = "provider=microsoft.jet.oledb.4.0;" & _
"extended properties='Excel 8.0;hdr=no';" & _
"data source=D:\Book2.xls"
.Open
For i = 1 To iRow
For j = 1 To iCol
strt = "update [Sheet1$" & Chr(97 + j) & (i + 1) & ":" & Chr(97 + j) & (i + 1) & "] set f1='" & arr(i, j) & "'"
.Execute strt
Next j
Next i
End With
CNN.Close
End Sub
[ 本帖最后由 lanny.mo 于 2011-1-7 00:49 编辑 ] |
|