![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Location: Horley Surrey England
Posts: 7
|
I have a sheet with numbers everywhere on it, is it possible for a macro to be in place so that when a user highlights a range (could be anything) excel will sum, count and average this range and put the answer in (A1, A2, A3).
Similar to what the toolbar at the bottom |
|
|
|
|
|
#2 |
|
Join Date: May 2002
Posts: 73
|
This probably needs a bit more sophistication, but just off the cuff try this :-
Private Sub Worksheet_SelectionChange(ByVal Target As Range) [A1].Value = Application.Sum(Selection) [A2].Value = Application.Count(Selection) [A3].Value = Application.Average(Selection) End Sub [ This Message was edited by: dimrod on 2002-05-23 05:17 ] |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Apr 2002
Location: Trussville, AL
Posts: 134
|
That is so cool.
Absolutely amazing. |
|
|
|
|
|
#4 | |
|
Join Date: May 2002
Posts: 1
|
Quote:
|
|
|
|
|
|
|
#5 |
|
Join Date: May 2002
Posts: 73
|
Thanks Ivan !
|
|
|
|
|
|
#6 |
|
Board Regular
Join Date: Apr 2002
Location: Trussville, AL
Posts: 134
|
Modified code to get rid of Divide by Zero error
[A1].Value = Application.Sum(Selection) [A2].Value = Application.Count(Selection) [A3].Value = IIf(Application.Count(Selection) = 0, 0, Application.Average(Selection)) -------------------------- Is there a way to toggle this on and off? |
|
|
|
|
|
#7 | ||
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
The code is good BUT I never made those comments.... Oh well ........ _________________ Kind Regards, Ivan F Moala From the City of Sails ![]() [ This Message was edited by: Ivan F Moala on 2002-05-23 07:32 ] |
||
|
|
|
|
|
#8 | |
|
Join Date: May 2002
Posts: 73
|
Quote:
Does this mean that I am not a genius ? I don't think the last line of code you posted is going to work. However, there are various ways you could toggle on/off. One way would be to have a macro that toggles between EnableEvents=True/EnableEvents=False. But this is not so good because it would affect all event procedures. Another way would be to have a hidden worksheet(let's say named "Hidden") and a macro(via a button, if you like) available to the relevant worksheet:- Sub ToggleIt() With Sheets("Hidden") If .[A1].Value = "Off" Then .[A1].Value = "On" Else .[A1].Value = "Off" End If End With End Sub Then the event procedure(in the relevant sheet) would be :- Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Sheets("Hidden").[A1].Value = "On" Then [A1].Value = Application.Sum(Selection) [A2].Value = Application.Count(Selection) If Application.Count(Selection) = 0 Then [A3].Value = 0 Else [A3].Value = Application.Average(Selection) End If End If End Sub I haven't tested the above, but anyway there are probably better ways that some of the aspiring genii on this board will no doubt advocate. |
|
|
|
|
|
|
#9 | |||
|
Join Date: May 2002
Posts: 2
|
Quote:
Hey, Ivan ! Just someone's little joke ! |
|||
|
|
|
|
|
#10 | ||||
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
|
||||
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|