Solved

Subtotal Function in an Excel Export from Access

Posted on 2008-10-30
7
924 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
Industry Leaders: 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

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)

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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