Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 14th, 2005, 10:49 PM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default Excel VBA Check When Cell Has Changed

Okay, maybe I'm just stupid ...

Let's say cell "A1" and "A2"

I want to check if "A2" changes and if so, run a macro ...

I can't get the event to fire off and, I'm sure it's because I have no clue what I'm doing.

Help ... please.

Reply With Quote
  #2 (permalink)  
Old November 15th, 2005, 02:41 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: , , Finland.
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI Tony

Put this code to Worksheet Change Event

Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("A2") Then
    'Do some coding here
End If
End Sub

-vemaju

Reply With Quote
  #3 (permalink)  
Old November 15th, 2005, 09:36 AM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default

Yea! I'm a superstar:

Sub Worksheet_Change(ByVal Target As Range)
    Dim WatchRange As Range
    Dim IntersectRange As Range
    Set WatchRange = Range("A1:A10")
    Set IntersectRange = Intersect(Target, WatchRange)
    If IntersectRange Is Nothing Then
        'Do Nothing Spectacular
    Else
        Call MyMacro
    End If
End Sub

Reply With Quote
The Following User Says Thank You to tonyrosen For This Useful Post:
  #4 (permalink)  
Old November 15th, 2005, 01:03 PM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default

Argh!

It won't call my Macro ... The Worksheet_Change works, but it won't call the macro ... is there a syntax for that?

Reply With Quote
  #5 (permalink)  
Old November 15th, 2005, 01:09 PM
Friend of Wrox
 
Join Date: Jan 2005
Location: Bournemouth, Dorset, United Kingdom.
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just a quickie,

above works fine for me, can you go into Excel and check the following...

In Menu [Tools]...[Macro]...[Security]

What setting do you have?

Matt

Reply With Quote
  #6 (permalink)  
Old November 15th, 2005, 02:23 PM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default

ub Worksheet_Change(ByVal Target As Range)
    Dim WatchRange As Range
    Dim IntersectRange As Range
    Set WatchRange = Range("A1:A10")
    Set IntersectRange = Intersect(Target, WatchRange)
    If IntersectRange Is Nothing Then
        'Do Nothing Spectacular
    Else
        Call MyMacro 'Where MyMacro is the name of my macro.
    End If
End Sub


Reply With Quote
  #7 (permalink)  
Old November 15th, 2005, 06:00 PM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default

Okay, here's my SUB

=============

Sub ValidateStopDate()
    MyRow = ActiveCell.Row
    'This is the Stop Date Column Number
    MyColumn = ActiveCell.Column
    'This is the Start Date Column Number
    MyColumnPrev = MyColumn - 1

    If MyColumn > 26 Then
        MyColumnLetter = Chr(Int((MyColumn - 1) / 26) + 64) & Chr(((MyColumn - 1) Mod 26) + 65)
    Else
        MyColumnLetter = Chr(MyColumn + 64)
    End If
    'MyColumnLetter = Mid(ActiveCell.Address, 2, (InStr(2, ActiveCell.Address, "$")) - 2)


    MyColumnNext = MyColumn + 1
    If MyColumnNext > 26 Then
        MyColumnLetterNext = Chr(Int((MyColumnNext - 1) / 26) + 64) & Chr(((MyColumnNext - 1) Mod 26) + 65)
    Else
        MyColumnLetterNext = Chr(MyColumnNext + 64)
    End If

    MyRange = Range(MyColumnLetter & MyRow)
    MyNextRange = Range(MyColumnLetterNext & MyRow)

    Range("H3") = MyNextRange
    'Range("G3") = MyColumnLetterNew

    'MyNewRange = Column F
    'MyRange = Column E
    'If Year(F) + 1 > Year(E)
    StopMonth = Month(MyRange)
    StopDay = Day(MyRange)
    StopYear = Year(MyRange)
    YearOne = MyNextRange

    If StopYear < YearOne Then
        'StopDate is no good
        MsgBox ("Your Stop Date cannot occur prior to 12/31/" & YearOne & ".")
    Else
        If StopYear = YearOne Then
            If StopMonth < 12 And StopDay < 31 Then
                'StopDate is no good
                MsgBox ("Your Stop Date cannot be sooner than 12/31/" & YearOne & ".")
            Else
                'StopDate is after 12/31/YearOne
                Range("H3") = "OKAY"
            End If
        End If
    End If
End Sub

=============

And, my Change ...

=============

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("F2")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        Call ValidateStopDate
    End If
End Sub

===================

It won't run the macro call ... help(?)

Reply With Quote
  #8 (permalink)  
Old November 16th, 2005, 10:32 AM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default

On top of that, if I place a button on the page to run the code, it works perfectly as well.

Reply With Quote
  #9 (permalink)  
Old November 16th, 2005, 11:36 AM
Authorized User
 
Join Date: Oct 2004
Location: , , .
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shattered Send a message via Yahoo to shattered
Default

need to evaluate whats going wrong here...

The procedure does work so two questions ?

1) is the code in the actual worksheet (ie not the workbook or a module)?

2) is calculation set at automatic?

Reply With Quote
  #10 (permalink)  
Old November 16th, 2005, 11:52 AM
Authorized User
 
Join Date: Nov 2005
Location: Houston, TX, .
Posts: 16
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to tonyrosen
Default

Figured it out ... I was "finding" the wrong cells. My "active" cell was REALLY the desired "active" cell plus one ... Here is the piece I fixed:

    MyRow = ActiveCell.Row
    'This is the Stop Date Column Number
    MyColumn = ActiveCell.Column - 1
    'This is the Start Date Column Number
    MyColumnPrev = MyColumn - 2

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I check if a cell content has changed SirAldemar Access VBA 2 August 13th, 2008 07:46 AM
How to check whether page data has changed webmahesh JSP Basics 0 December 12th, 2007 04:38 AM
Changing Cell-formating in Excel via VBA Gert VB How-To 4 May 10th, 2005 01:56 PM
How do I get the CHANGED value in a datagrid cell? Ron Howerton VB.NET 2002/2003 Basics 2 December 23rd, 2004 10:51 AM



All times are GMT -4. The time now is 08:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.