#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default Accumulator Logic

hi,
I'm using the following code taken from "McGimpsey & Associates" site and it
works nicely for me but I'm not able to undrestand how the code works and
what its logic is.can anybody explain to more about this codes,particulary
the line "Application.EnableEvents = False or true"?thanx.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
Next
End With
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 772
Default Accumulator Logic

Looks like bad code to me :)
the next should be an end if
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
End If
End With
End Sub

i wouldn't know why enableevents would matter unless there is more code
somewhere.
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"peyman" wrote:

hi,
I'm using the following code taken from "McGimpsey & Associates" site and it
works nicely for me but I'm not able to undrestand how the code works and
what its logic is.can anybody explain to more about this codes,particulary
the line "Application.EnableEvents = False or true"?thanx.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
Next
End With
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Accumulator Logic

I can probably help:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then


We only operate on the accumulator if the changed range is the single
cell D1.

If IsNumeric(.Value) Then


Checks that the entry is a number

Application.EnableEvents = False


Since the next line will change the value in C1, if we don't disable
events, doing so will fire the Worksheet_Change code again. Disabling
events allows us to change the C1 value without triggering the _Change
event.

Range("c1").Value = Range("c1").Value + .Value


Adds the value in D1 to the value in C1

Application.EnableEvents = True


Turns on Event macros again.

End If
Next
End With
End Sub


In article ,
peyman wrote:

hi,
I'm using the following code taken from "McGimpsey & Associates" site and it
works nicely for me but I'm not able to undrestand how the code works and
what its logic is.can anybody explain to more about this codes,particulary
the line "Application.EnableEvents = False or true"?thanx.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
Next
End With
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Accumulator Logic

Just prevents firing the _Change macro again when the change is made to
C1.

In article ,
John Bundy (remove) wrote:

i wouldn't know why enableevents would matter unless there is more code
somewhere.

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default Accumulator Logic

Sorry John, because I extracted part of the codes.the "next" must be omitted
here.
thank you.

"John Bundy" wrote:

Looks like bad code to me :)
the next should be an end if
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
End If
End With
End Sub

i wouldn't know why enableevents would matter unless there is more code
somewhere.
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"peyman" wrote:

hi,
I'm using the following code taken from "McGimpsey & Associates" site and it
works nicely for me but I'm not able to undrestand how the code works and
what its logic is.can anybody explain to more about this codes,particulary
the line "Application.EnableEvents = False or true"?thanx.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
Next
End With
End Sub



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default Accumulator Logic

Thanks a lot

"JE McGimpsey" wrote:

I can probably help:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then


We only operate on the accumulator if the changed range is the single
cell D1.

If IsNumeric(.Value) Then


Checks that the entry is a number

Application.EnableEvents = False


Since the next line will change the value in C1, if we don't disable
events, doing so will fire the Worksheet_Change code again. Disabling
events allows us to change the C1 value without triggering the _Change
event.

Range("c1").Value = Range("c1").Value + .Value


Adds the value in D1 to the value in C1

Application.EnableEvents = True


Turns on Event macros again.

End If
Next
End With
End Sub


In article ,
peyman wrote:

hi,
I'm using the following code taken from "McGimpsey & Associates" site and it
works nicely for me but I'm not able to undrestand how the code works and
what its logic is.can anybody explain to more about this codes,particulary
the line "Application.EnableEvents = False or true"?thanx.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
Next
End With
End Sub


  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default Accumulator Logic

Sorry,what do you mean by "fire the worksheet_chnage"?

"JE McGimpsey" wrote:

I can probably help:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then


We only operate on the accumulator if the changed range is the single
cell D1.

If IsNumeric(.Value) Then


Checks that the entry is a number

Application.EnableEvents = False


Since the next line will change the value in C1, if we don't disable
events, doing so will fire the Worksheet_Change code again. Disabling
events allows us to change the C1 value without triggering the _Change
event.

Range("c1").Value = Range("c1").Value + .Value


Adds the value in D1 to the value in C1

Application.EnableEvents = True


Turns on Event macros again.

End If
Next
End With
End Sub


In article ,
peyman wrote:

hi,
I'm using the following code taken from "McGimpsey & Associates" site and it
works nicely for me but I'm not able to undrestand how the code works and
what its logic is.can anybody explain to more about this codes,particulary
the line "Application.EnableEvents = False or true"?thanx.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "D1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
Range("c1").Value = Range("c1").Value + .Value
Application.EnableEvents = True
End If
Next
End With
End Sub


  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Accumulator Logic

Cause excel to execute the Worksheet_Change macro.

If a worksheet cell is changed, even if it's by the Worksheet_Change()
macro, the macro will be put on hold, and the Worksheet_Change macro
called with the new Target.

In this case, the second call will exit without doing anything (since
the new Target will be cell C1 rather than cell D1) before resuming
execution of the first instance.

But there's no reason for it to be executed at all.

In article ,
peyman wrote:

Sorry,what do you mean by "fire the worksheet_chnage"?

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
accumulator with other than one dan Excel Worksheet Functions 8 November 24th 06 06:29 PM
Date STOP Accumulator Michell Major Excel Discussion (Misc queries) 3 October 19th 06 10:51 AM
Need help with two cell accumulator dlashley Excel Worksheet Functions 1 May 12th 06 11:02 AM
Two cell accumulator Bill Excel Worksheet Functions 4 August 21st 05 09:07 PM
Two cell accumulator Bill Excel Worksheet Functions 1 January 3rd 05 03:15 PM


All times are GMT +1. The time now is 03:49 AM.

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"