?
Solved

Pull Sum of Excel Range into Access using VBA

Posted on 2013-12-16
4
Medium Priority
?
988 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 1500 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

764 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