|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 vip-hai 于 2023-6-15 11:52 编辑
vba access数据导入excel时存在空行,
代码如下
Sub 插入汇总()
'建立数据库连接
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
With cnn
.Provider = "microsoft.ace.oledb.12.0"
.Open ThisWorkbook.Path & "\datebase.accdb"
End With
Dim mytable As String
mytable = "cheribao" '指定数据表名
Dim row As Integer, n As Integer, i As Integer, j As Integer, SQL As String, arr As Variant
arr = Array("施工数量", "前挡升级数量", "前挡升级金额", "侧挡升级数量", "侧挡升级金额", "车衣装贴数量", "车衣装贴金额", "其他金额")
'月汇总数据
For j = 0 To UBound(arr)
SQL = "select sum(长安), sum(大众), sum(红旗),sum(哈弗),sum(东风),sum(本田),sum(日产), sum(江铃)," _
& "sum(五十铃),sum(吉利),sum(北现),sum(标致),sum(丰田),sum(传奇),sum(福田),sum(其他1)," _
& "sum(其他2),sum(其他3),sum(散客) from " & mytable _
& " where year(日期)= " & Year(Date) & " And Month(日期) = " & Month(Date) & " And 项目 ='" & arr(j) & "'"
' MsgBox arr(j)
rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic
Range("d" & j * 2 + 12).CopyFromRecordset rst
rst.Close
Next j
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Sub
|
|