Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 29
Default VBA Code when a cell is selected

I have a simple worksheet that has two columns of data, column A is
month and year, column B is a four digit number. What I'm looking to
do is get an average when a cell is selected. The average would
populate in cell D3. So, let say I click or select cell B18, the
formula should be something like =average(b7:b18) for cell D3. In
other words when a cell is select that the average counts back 12
cells up. I'm looking to get the average number for a 12 month span.
If I select cell B19, then the code should be for an average
=average(b8:b19). Make sense?

Any ideas on some vba code that would do this?

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,339
Default VBA Code when a cell is selected

Try:


'-----------------------------------------------------------------
Private Sub Worksheet_Selectionchange(ByVal Target As Range)
'-----------------------------------------------------------------
Const WS_RANGE As String = "B1:B1000" '<===Change as required


On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Row = Target.Row
col = Target.Column
Range("D3") = Application.Average(Cells(Row, col), Cells(Row -
11, col))
End With
End If


ws_exit:
Application.EnableEvents = True
End Sub

Right-click on sheet tab, view code and copy/paste code above

" wrote:

I have a simple worksheet that has two columns of data, column A is
month and year, column B is a four digit number. What I'm looking to
do is get an average when a cell is selected. The average would
populate in cell D3. So, let say I click or select cell B18, the
formula should be something like =average(b7:b18) for cell D3. In
other words when a cell is select that the average counts back 12
cells up. I'm looking to get the average number for a 12 month span.
If I select cell B19, then the code should be for an average
=average(b8:b19). Make sense?

Any ideas on some vba code that would do this?


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,123
Default VBA Code when a cell is selected

You can use this event in the sheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Range("B13:B30"), Target) Is Nothing Then
Range("D3").Formula = "=average(b" & Target.Row - 11 & ":b" & Target.Row & ")"
End If
End Sub

Change B13:B30 to the range that must activate the code


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


wrote in message oups.com...
I have a simple worksheet that has two columns of data, column A is
month and year, column B is a four digit number. What I'm looking to
do is get an average when a cell is selected. The average would
populate in cell D3. So, let say I click or select cell B18, the
formula should be something like =average(b7:b18) for cell D3. In
other words when a cell is select that the average counts back 12
cells up. I'm looking to get the average number for a 12 month span.
If I select cell B19, then the code should be for an average
=average(b8:b19). Make sense?

Any ideas on some vba code that would do this?

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 29
Default VBA Code when a cell is selected

On Jun 13, 2:14 pm, "Ron de Bruin" wrote:
You can use this event in the sheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Range("B13:B30"), Target) Is Nothing Then
Range("D3").Formula = "=average(b" & Target.Row - 11 & ":b" & Target.Row & ")"
End If
End Sub

Change B13:B30 to the range that must activate the code

--

Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm



wrote in ooglegroups.com...
I have a simple worksheet that has two columns of data, column A is
month and year, column B is a four digit number. What I'm looking to
do is get an average when a cell is selected. The average would
populate in cell D3. So, let say I click or select cell B18, the
formula should be something like =average(b7:b18) for cell D3. In
other words when a cell is select that the average counts back 12
cells up. I'm looking to get the average number for a 12 month span.
If I select cell B19, then the code should be for an average
=average(b8:b19). Make sense?


Any ideas on some vba code that would do this?- Hide quoted text -


- Show quoted text -


Thanks for the comments, I used the one from Ron de and it worked very
well....thanks a ton!

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
deleting selected worksheets using vb code George Excel Discussion (Misc queries) 1 October 31st 06 05:14 PM
How to change cell text based on another selected cell? jjh Excel Discussion (Misc queries) 1 July 6th 06 01:14 PM
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Excel Worksheet Functions 6 March 29th 06 12:43 PM
automatic offset cell location when there is a value in a selected cell. kuansheng Excel Worksheet Functions 0 February 23rd 06 01:40 AM
how read value from last selected cell? It is possible? how get adress last selected cell? Andrzej New Users to Excel 4 May 30th 05 07:28 PM


All times are GMT +1. The time now is 06:24 PM.

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

About Us

"It's about Microsoft Excel"