Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default How can I use VBA to SubTotal


Hi,

Here's one way you can try. It requires that you add a reference to the
Excel in your project (Tools References Microsoft Excel 8.0 Object
Library).
After the query has been exported to Excel, it opens it (instead of
being opened by Application.Hyperlink). A date variable is created for the
Save As and then it's sorted & sub-totaled & closed.
The original file is then re-opened and the query results deleted.
Hopefully this works for you, or at least helps.


Sub Ex1()

Dim stDocName As String
stDocName = "C:\VB 2008\CallDetail.xls"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
"qryCallDetails", stDocName, , "WeeklyCalls"

Dim xl As Excel.Application
Dim xlwb As Excel.Workbook

Set xl = New Excel.Application
Set xlwb = xl.Workbooks.Open(stDocName)

xl.Visible = True

Dim dtSave As String
dtSave = Format(Now(), "mmddyy")
xlwb.SaveAs ("C:\VB 2008\CallDetail" & dtSave & ".xls")

Dim WeeklyCalls As Range
Set WeeklyCalls = xlwb.Sheets("WeeklyCalls").Range("A1").CurrentRegi on

With xlwb.Sheets("WeeklyCalls").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("A:A"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.SetRange WeeklyCalls
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With

With xlwb.Sheets("WeeklyCalls").Range("WeeklyCalls")
.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End With

xlwb.Close SaveChanges:=True

Set xlwb = xl.Workbooks.Open(stDocName)
xl.DisplayAlerts = False
xlwb.Sheets("WeeklyCalls").Delete
xl.DisplayAlerts = True
xlwb.Close SaveChanges:=True

xl.Quit

Set xl = Nothing

End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtotal To Include Item Description On Subtotal Line Tickfarmer Excel Discussion (Misc queries) 2 February 23rd 10 07:56 PM
pasting to subtotal lines without replacing hidden -non-subtotal l harleydiva67 Excel Discussion (Misc queries) 1 October 12th 06 06:02 PM
Bolding the subtotal lines automaticlly When using the Subtotal fu 06Speed6 New Users to Excel 2 October 5th 06 03:52 PM
Subtotal of Subtotal displays Grand Total in wrong row Thomas Born Excel Worksheet Functions 5 January 6th 05 01:46 PM
Sort, Subtotal, Label Subtotal, Insert row Teak Excel Programming 2 April 8th 04 04:14 PM


All times are GMT +1. The time now is 11:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"