View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
David David is offline
external usenet poster
 
Posts: 153
Default Subtotal Macro weird actions

Hi-This should work. Subtotal is off of the activecell.

Sub SUBTOTAL()
'- select range and run macro
Dim MyRange As Range
Set MyRange = Selection
OffsetValue = MyRange.Rows.Count
If ActiveCell.Row MyRange.Row Then
ActiveCell.Offset(1, 0).Formula = _
"=SUBTOTAL(9," & MyRange.Address & ")"
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(MyRange.Rows.Count, 0).Formula = _
"=SUBTOTAL(9," & MyRange.Address & ")"
ActiveCell.Offset(MyRange.Rows.Count, 0).Select
End If
' moves to bottom of selection where subtotal was
just inserted.
End Sub

-----Original Message-----
I have a macro that I use to subtotal lines (I took

parts of this from
other macros on this group and elsewhere and don't know

who to give
credit to, so thanks whoever you are)

Sub SUBTOTAL()
'- select range and run macro
Dim MyRange As Range
Set MyRange = Selection
ActiveCell.Offset(MyRange.Rows.Count, 0).Formula = _
"=SUBTOTAL(9," & MyRange.Address & ")"
ActiveCell.Offset(MyRange.Rows.Count, 0).Select
' moves to bottom of selection where subtotal was

just inserted.
End Sub

As an example I have numbers in B1:B10, I highlight from

B1 down to
B10, and hit a button to run the macro. It puts the

subtotal in B11.
So far so good.

If I highlight from B10 up to B1, it puts the subtotal

in B20.

How do I amend so that I can have it work no matter

where I start to
highlight?
Can I do it to subtotal across, and total on the right?

And even
better let the macro know if it up/down or across and

put the subtotal
in the right place.

Thanks so much.
.