Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2005, 10:49 PM
Authorized User
 
Join Date: Nov 2005
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.

 
Old November 15th, 2005, 02:41 AM
Friend of Wrox
 
Join Date: Oct 2003
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

 
Old November 15th, 2005, 09:36 AM
Authorized User
 
Join Date: Nov 2005
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

The Following User Says Thank You to tonyrosen For This Useful Post:
 
Old November 15th, 2005, 01:03 PM
Authorized User
 
Join Date: Nov 2005
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?

 
Old November 15th, 2005, 01:09 PM
Friend of Wrox
 
Join Date: Jan 2005
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

 
Old November 15th, 2005, 02:23 PM
Authorized User
 
Join Date: Nov 2005
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


 
Old November 15th, 2005, 06:00 PM
Authorized User
 
Join Date: Nov 2005
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(?)

 
Old November 16th, 2005, 10:32 AM
Authorized User
 
Join Date: Nov 2005
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.

 
Old November 16th, 2005, 11:36 AM
Authorized User
 
Join Date: Oct 2004
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?

 
Old November 16th, 2005, 11:52 AM
Authorized User
 
Join Date: Nov 2005
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






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





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