excel - SUM data from recordset in VBA - Stack Overflow

The idea is to pull data from closed workbook, so I have this:Option ExplicitSub DataIzZatvorenogFila

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
  • Might be easier to use Power Query to get the data? Available through Data > Get & Transform Data – Darren Bartrup-Cook Commented Mar 20 at 12:50
  • What I need is the SUM of those 12 consecutive cells which are in Recordset, to put in "D2" WIll this not overwrite the value in D2 you just put there with Sheet3.Range("D2").CopyFromRecordset rst? – Storax Commented Mar 20 at 12:58
  • That is how it functioning now, but I don't need whole range, just SUM of the range! – Jelovac Maglaj Commented Mar 20 at 13:12
Add a comment  | 

1 Answer 1

Reset to default 2

If 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744408987a4572778.html

相关推荐

  • excel - SUM data from recordset in VBA - Stack Overflow

    The idea is to pull data from closed workbook, so I have this:Option ExplicitSub DataIzZatvorenogFila

    6天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信