Solved

Pull Sum of Excel Range into Access using VBA

Posted on 2013-12-16
4
956 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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