admin管理员组文章数量:1403493
The idea is to pull data from closed workbook, so I have this:
Option Explicit
Sub DataIzZatvorenogFila()
Dim con As ADODB.Connection
Dim rst As ADODB.Recordset
Set con = New ADODB.Connection
Set rst = New ADODB.Recordset
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source= W:\Materijalno\MG i AMBALAŽNI PAPIR\2025\FLUTING\FLUTING.xlsm; " & _
"Extended Properties='Excel 12.0 Xml;HDR=NO';"
con.Open
rst.ActiveConnection = con
rst.Source = "[ZBIRNA$E29:E40]"
rst.Open
Sheet3.Range("D2").CopyFromRecordset rst
rst.Close
con.Close
End Sub
It puls data from closed WB/sheetname(ZBIRNA)/range(E29:E40) and paste it in active WB/Sheet3/cells D2 to D13.
What I need is the SUM of those 12 consecutive cells which are in Recordset, to put in "D2"
I'm not very familiar with Databases and sql commands(not sure if I need it at all), saw someone mentioning DSUM function, but I don't know how to incorporate that here.
The idea is to pull data from closed workbook, so I have this:
Option Explicit
Sub DataIzZatvorenogFila()
Dim con As ADODB.Connection
Dim rst As ADODB.Recordset
Set con = New ADODB.Connection
Set rst = New ADODB.Recordset
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source= W:\Materijalno\MG i AMBALAŽNI PAPIR\2025\FLUTING\FLUTING.xlsm; " & _
"Extended Properties='Excel 12.0 Xml;HDR=NO';"
con.Open
rst.ActiveConnection = con
rst.Source = "[ZBIRNA$E29:E40]"
rst.Open
Sheet3.Range("D2").CopyFromRecordset rst
rst.Close
con.Close
End Sub
It puls data from closed WB/sheetname(ZBIRNA)/range(E29:E40) and paste it in active WB/Sheet3/cells D2 to D13.
What I need is the SUM of those 12 consecutive cells which are in Recordset, to put in "D2"
I'm not very familiar with Databases and sql commands(not sure if I need it at all), saw someone mentioning DSUM function, but I don't know how to incorporate that here.
Share Improve this question asked Mar 20 at 12:42 Jelovac MaglajJelovac Maglaj 337 bronze badges 3 |1 Answer
Reset to default 2If you only need the SUM in D2, I'd suggest to use the following code
Option Explicit
Sub DataIzZatvorenogFila()
Dim con As ADODB.Connection
Dim rst As ADODB.Recordset
Dim total As Variant
Set con = New ADODB.Connection
Set rst = New ADODB.Recordset
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source= W:\Materijalno\MG i AMBALAŽNI PAPIR\2025\FLUTING\FLUTING.xlsm; " & _
"Extended Properties='Excel 12.0 Xml;HDR=NO';"
con.Open
' Use SQL to sum the values in the specified range
rst.Open "SELECT SUM(F1) AS Total FROM [ZBIRNA$E29:E40]", con
' Retrieve the total from the recordset
If Not rst.EOF Then
total = rst.Fields("Total").Value
Else
total = 0 ' In case there are no records
End If
' Close the recordset and connection
rst.Close
con.Close
' Place the total in cell D2 of Sheet3
Sheet3.Range("D2").Value = total
End Sub
Update
F1
refers to the first column of the specified range in the Excel worksheet.
本文标签: excelSUM data from recordset in VBAStack Overflow
版权声明:本文标题:excel - SUM data from recordset in VBA - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744408987a2604853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Sheet3.Range("D2").CopyFromRecordset rst
? – Storax Commented Mar 20 at 12:58