Solved

Subtotal Function in an Excel Export from Access

Posted on 2008-10-30
7
903 Views
Last Modified: 2013-11-27
Hello Experts,
I am exporting a table from Access to Excel - preformatted with functions.  The export works great.  I now must SUBTOTAL on certain fields before formatting.  I have attached export function here.  I want to now Subtotal on the fields CCTS down thru 2005. I want a grand total at the end.

I found the term Subtotal in the VBA Object Browser so I think it can be done.  After subtotaling I will format the sheet - that part is easy.  Any help in coding the Subtotalwould be appreciated.
Function AutoActual()
Dim rstCurr As New ADODB.Recordset
Dim nRow As Long
Dim appExcel As Excel.Application
Dim wbkNew As Excel.Workbook
Dim wksNew As Excel.Worksheet
Dim rngCurr As Excel.Range

rstCurr.Open "tblCurrent", CurrentProject.Connection, adOpenStatic

nRow = rstCurr.RecordCount

Set appExcel = Excel.Application
Set wbkNew = appExcel.Workbooks.Add
Set wksNew = wbkNew.Worksheets.Add

With wksNew
    .Cells(4, 2).Value = "SAPID"
    .Cells(4, 3).Value = "Tank"
    .Cells(4, 4).Value = "Supplier"
    .Cells(4, 5).Value = "CCTS"
    .Cells(4, 6).Value = "Dec"
    .Cells(4, 7).Value = "Nov"
    .Cells(4, 8).Value = "Oct"
    .Cells(4, 9).Value = "Sep"
    .Cells(4, 10).Value = "Aug"
    .Cells(4, 11).Value = "Jul"
    .Cells(4, 12).Value = "Jun"
    .Cells(4, 13).Value = "May"
    .Cells(4, 14).Value = "Apr"
    .Cells(4, 15).Value = "Mar"
    .Cells(4, 16).Value = "Feb"
    .Cells(4, 17).Value = "Jan"
    .Cells(4, 18).Value = "2007"
    .Cells(4, 19).Value = "2006"
    .Cells(4, 20).Value = "2005"
    .Cells(4, 21).Value = "Supplier"
End With



rstCurr.MoveLast
rstCurr.MoveFirst

Set rngCurr = wksNew.Range(wksNew.Cells(5, 2), wksNew.Cells(5 + nRow, 20))
rngCurr.CopyFromRecordset rstCurr
wksNew.Range(Cells(5, 3), Cells(5 + nRow, 3)).Select

appExcel.Visible = True

Set appExcel = Nothing
Set wbkNew = Nothing
Set wksNew = Nothing
Set rstCurr = Nothing
End Function

0
Comment
Question by:bleleux
  • 4
  • 3
7 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
here is an example

    With .Range("A1").CurrentRegion
        .SubTotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, 8, 9, 10, _
        11, 12, 13, 14, 15), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    End With

that will create subtotals for columns 4 to 15
0
 
LVL 1

Author Comment

by:bleleux
Comment Utility
This is a great example but, I cannot make it work.  With . Range returns an error "Invalid or Unqualified Reference".  I tried everything - can you explain?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
try
with wksNew
    With .Range("A1").CurrentRegion
        .SubTotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, 8, 9, 10, _
        11, 12, 13, 14, 15), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    End With
end with
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:bleleux
Comment Utility
Error Message: "Subtotal Method of Range Class Fails" .  There is something wrong with the syntax .
          .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, 8, 9, 10, _
        11, 12, 13, 14, 15), Replace:=True, PageBreaks:=False, SummaryBelowData:=True

Can you help again?





0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
one more try

with appExcel
    With .Range("A1").CurrentRegion
        .SubTotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, 8, 9, 10, _
        11, 12, 13, 14, 15), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    End With
end with
0
 
LVL 1

Author Comment

by:bleleux
Comment Utility
Yee-Hah! It worked.  This is what I did:

With wksNew
    With .Range("A4:A15").CurrentRegion
        .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, 8, 9, 10, _
        11, 12, 13, 14, 15), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    End With
End With

You are an EXPERT!
0
 
LVL 1

Author Closing Comment

by:bleleux
Comment Utility
Good Work!  I appreciate the time and expediency of your answer.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now