Solved

Pull Sum of Excel Range into Access using VBA

Posted on 2013-12-16
4
913 Views
Last Modified: 2013-12-16
I am opening an excel spreadsheet from Access, and need to sum up a range on Worksheet(1), range G13:G100. I have been able to grab the data from a specific cell and bring it in, per the code below, but how do I get the sum of a range?
 
I have tried:
rec.Fields("TotalTime") = xlsSht.WorksheetFunction.Sum("G13:G100")
 
but am getting the error "Run-time error '438': Object doesn't support this property or method."

Private Sub cmdImport_Click()
Dim rec As DAO.Recordset
Dim xls As Object
Dim xlsSht As Object
Dim xlsWrkBk As Object
Dim xlsPath As String
Dim xlsFile As String
Dim fullXlsFile As String
Dim fullFile As String
 
Dim Msg, Style, title, Response
Msg = "Importing is Done, Files are imported!" ' Define message.
Style = vbOKOnly
title = "Import Mesage"

xlsPath = Me.txtFolder & "\"
xlsFile = Dir(xlsPath & "*.xls", vbNormal) ' Retrieve the first entry.

Do While xlsFile <> ""
' Ignore the current directory and the encompassing directory.
fullXlsFile = xlsPath & xlsFile
fullFile = xlsPath & xlsFile

If Right(fullXlsFile, 4) = ".xls" Then 'import it
DoCmd.SetWarnings False
Set xls = CreateObject("Excel.Application")
xls.Application.Visible = False
xls.Application.displayalerts = False
Set xlsWrkBk = GetObject(fullXlsFile)
Set xlsSht = xlsWrkBk.Worksheets(1) 'NestSummary
 
'open table
Set rec = CurrentDb.OpenRecordset("tblNestImport")
rec.AddNew
rec.Fields("NestID") = Nz(StrConv(xlsSht.cells(2, "E"), vbProperCase), "badnestid")
rec.Fields("MaterialPartNumber") = Nz(StrConv(xlsSht.cells(5, "N"), vbProperCase), "badmaterialpartnumber")
rec.Fields("TotalTime") = xlsSht.WorksheetFunction.Sum("G13:G100")

rec.Update

DoCmd.SetWarnings True
End If

'close excel
xlsWrkBk.Application.Quit
Set xls = Nothing

xlsFile = Dir()
Loop

Response = MsgBox(Msg, Style, title)

End Sub

Open in new window

0
Comment
Question by:maverick0728
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39721407
You're close, you just need to put a range object in the .Sum function:
rec.Fields("TotalTime") = xlsSht.WorksheetFunction.Sum(xlsSht.Range("G13:G100"))

Open in new window

Matt
0
 

Author Closing Comment

by:maverick0728
ID: 39721425
I ended up using:
xls.WorksheetFunction.Sum(xlsSht.Range("G13:G100"))

replacing xlsSht with xls in the beginning.

Thanks for your help!!
0
 
LVL 35

Expert Comment

by:mvidas
ID: 39721455
Whoops, I didn't even notice that you had used xlsSht to call worksheetfunction, I just copied that.

For what its worth, if you're a code minimalist, you can also just use xls.Sum(...) and skip the worksheetfunction part. It also has been known to resolve a random error at times ( http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_27607747.html )
0
 

Author Comment

by:maverick0728
ID: 39721633
Thanks Matt!  I'm going to try xls.Sum(...)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question