Solved

Subtotal Function in an Excel Export from Access

Posted on 2008-10-30
7
910 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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

813 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

19 Experts available now in Live!

Get 1:1 Help Now