Solved

Pull Sum of Excel Range into Access using VBA

Posted on 2013-12-16
4
949 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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 Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

740 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