Solved

Subtotal Function in an Excel Export from Access

Posted on 2008-10-30
7
917 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
[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
  • 4
  • 3
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22843020
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
ID: 22843192
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22843256
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
Independent Software Vendors: 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!

 
LVL 1

Author Comment

by:bleleux
ID: 22843395
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 22843438
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
ID: 22843505
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
ID: 31511755
Good Work!  I appreciate the time and expediency of your answer.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access, How to create variable 9 38
access to sql migration 5 24
ms/link form - subform field not recognised 3 16
Balance After Payment 12 16
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server views 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 Access…
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…

726 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