LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 252
Default Problem with Running VBA code on Cell Change

Marty,
I'm not following the part about AAA, BBB..... but this should get you
started:


In the ThisWorkbook module:

Private Sub Worksheet_Change(ByVal Target as Range)

' Eliminate cases of changing more than one cell at a time
If Target.Count1 then Exit Sub

'Make sure the change is to any cell in MyRange: E7:H31
If Not Intersect(Target,Range("E7:H31") Is Nothing Then
'Prevent this event from triggering itself over and over in a loop
Application.EnableEvents=False

Call YourMacro 'which is in a Standard Module
here is where the Range("J2")=DIST variable
comes in
End If

'Turn Events back on--this is not automatic like screen updating is
Application.EnableEvents=True
End Sub


"Marty" wrote:

Hello:

I'm using Excel 2003. I'm wanting to execute a series of code steps upon a
change to any cell within a specified range of cells. From some searching, I
found the following code example, which does what it says it's supposed to do:
================================================== ===
'This procedure will change the value of F10 to TRUE if the value in A1 is
changed
'to a value greater than or equal to 10. Note that we change
'Application.EnableEvents to False in order to prevent the Change event from
'calling itself, and then restore the setting back to True.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$A$1" Then
Application.EnableEvents = False
If Target.Value = 10 Then
Range("F10").Value = True
Else
Range("F10").Value = False
End If
Application.EnableEvents = True
End If
End Sub
================================================== ===
Seems simple enough. So I reasoned that I could delete the If-Else-End-If
block and replace it with my code that I want to execute, and change the
range from $A1$1 to the range of cells on which I want to activate my code if
it changes. In my case, this is "E7:H31"

I can't get it to work. Basically, I want to put the new value of DIST into
cell J2 if any of AAA, BBB or CCC changes.

Here is A SMALL PORTION of the code (NOTE: BBB, CCC and DDD are within the
range of cells that I want to monitor and execute upon changes. AAA and MDC
are only one of four fixed values and are outside the range. MDC is a
constant, also outside the range):
================================================== ===
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "E7:H31" Then
Application.EnableEvents = False
Dim MYSHEET as Object
Set MYSHEET = Workbooks(ActiveWorkbook.Name).Sheets("Sheet1")

If AAA = 0 And BBB = "M" And CCC = "3D" And DDD = "M" Then
DIST = {formula which contains AAA, BBB, CCC and DDD as variables}
If DIST < 0 Then
DIST = 0
End If
If DIST MDC Then
DIST = MDC
End If
MYSHEET.Cells(2, 10) = DIST
End If

Application.EnableEvents = True
End If
End Sub
================================================== ==
I get no errors, but I also get no results in J2 (I know the IF AAA = 0...
conditions are satisfied). Can anyone see what I'm doing wrong?

Thanks in advance.

MARTY

 
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
Running a macro on cell value change Brettjg Excel Discussion (Misc queries) 2 March 28th 07 02:55 PM
Running a macro on cell value change Mike Excel Discussion (Misc queries) 0 March 28th 07 01:13 AM
Running a macro on cell value change Brettjg Excel Discussion (Misc queries) 0 March 28th 07 12:08 AM
Running code on a drop down selection change Steve Haack Excel Worksheet Functions 1 April 26th 05 05:03 AM
Check cell for data before running code Pat Excel Programming 1 January 12th 05 08:58 PM


All times are GMT +1. The time now is 09:51 AM.

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"