Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default Toggle Subtotal on and off

I'm trying to create a button on a worksheet that will "toggle"
Subtotal on and off. I came up with this code, but it doesn't work.
The 2nd line (If Selection.SubTotal = False Then) is the offending
line. What would be the correct syntax?

Sub SubtotalOnOff()
Range("A8").Select

If Selection.SubTotal = False Then

Selection.SubTotal GroupBy:=3, Function:=xlSum,
TotalList:=Array(5, 6), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
ActiveSheet.Outline.ShowLevels RowLevels:=2

Else

Selection.RemoveSubtotal

End If

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default Toggle Subtotal on and off

Paul,

Because Subtotal is a Method rather than a property it doesn't have a
'state' that you can check in this way. A fairly easy way round this
would be to toggle something else at the same time as you apply or
remove Subtotal e.g. the forecolor of the button font or maybe the
button caption from [ Apply Subtotals ] to [ Remove Subtotals ]. This
would then give you a 'state' to check at the top of your routine.

Alternatively how about creating a name e.g. SubtotalState that you
toggle from ="ON" to ="OFF"


Sub SubtotalOnOff()

If ActiveWorkbook.Names("SubtotalState").RefersTo = "=""OFF""" Then


Range("A8").SubTotal GroupBy:=3, Function:=xlSum,
TotalList:=Array(5, 6), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
ActiveSheet.Outline.ShowLevels RowLevels:=2
ActiveWorkbook.Names("SubtotalState").RefersTo = "=""ON"""

Else

Selection.RemoveSubtotal
ActiveWorkbook.Names("SubtotalState").RefersTo = "=""OFF"""

End If

End Sub


I've not tested this but it should work - don't forget, you will need
to create the name first.

HTH
NickH

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default Toggle Subtotal on and off

Nick,

ABSOLUTELY BRILLIANT! Works perfectly! Thank you so very much!

Paul

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
sort macro, subtotal and add lines after subtotal David Excel Discussion (Misc queries) 1 August 29th 09 10:56 AM
pasting to subtotal lines without replacing hidden -non-subtotal l harleydiva67 Excel Discussion (Misc queries) 1 October 12th 06 06:02 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 12:52 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"