?
Solved

Subtotal Function in an Excel Export from Access

Posted on 2008-10-30
7
Medium Priority
?
931 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

765 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