Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Run subroutine when a cell changes - VBA in Excel

Posted on 2011-03-05
9
639 Views
Last Modified: 2012-05-11
I want to run a routine that updates a graph when a specific cell is changed

At the moment, I can't get the Workbook_SheetChange to fire

To test it, I put the following in the sheet containing the cell that is changed by the user, just taken from the VBA help file

Private Sub Workbook_SheetChange(ByVal Target As Range)

    MsgBox ("A change in cell " & Target.Address)
    
End Sub

Open in new window


Then to try to make sure events are enabled, I put the following in the Workbook area

Private Sub Workbook_Open()

    Application.EnableEvents = True
    
End Sub

Open in new window


I saved the file and reopened it, which I presume will have triggered EnableEvents but nothing happens whe I change the value in the sheet concerned, or any other value on that sheet.

How do I get Workbook_Open to be triggered?
0
Comment
Question by:sjgrey
9 Comments
 
LVL 12

Assisted Solution

by:Amick
Amick earned 75 total points
ID: 35043235
Change your SheetChange to this:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
   MsgBox ("A change in cell " & Target.Address)
End Sub

Open in new window


Tested on Excel 2007
0
 
LVL 1

Author Comment

by:sjgrey
ID: 35043278
Thanks but that didn't make any difference

I'm running Excel 2007 SP2
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 35043326
1) Did you see a toolbar saying "Security warning" and have an option button when you open the Excel file? If yes, click the option button and click "Enable this content".
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:sjgrey
ID: 35043359
No there were no warnings and other VBA code in the file runs without any problem
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 75 total points
ID: 35043380
The code you quoted is used when the cell's focus is changed (e.g. when your cell is focused at A1 and you enter some value and press "Enter" key on the keyboard, or you move your mouse to click on other cell.)

If you enter some value in the cell and click tick button. This will not trigger. What you need is calling Worksheet_Change function, like the following:


Private Sub Workbook_Change(ByVal Target As Range)

    MsgBox ("A change in cell " & Target.Address)
    
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:sjgrey
ID: 35043550
That's what I had for my code in the first place - see initial post

I have been pressing Enter to change a value but just tried the tick and that doesn't do it either.

Something else must be wrong I think
0
 
LVL 6

Accepted Solution

by:
TinTombStone earned 350 total points
ID: 35043665
This is the Workbook_SheetChange event

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
         MsgBox " Worksheet: " & Sh.Name & ", Cell: " & Target.Address & ", changed to: " & _ Range(Target.Address).Value
End Sub


This is the WorkSheet_Change event

Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "Cell: " & Target.Address & " changed to: " & Range(Target.Address).Value
End Sub


Both serve a similar pupose however Workbook_SheetChange is passed the changed sheet as an argument

The Sheet event triggers before the Workbook event
0
 
LVL 1

Author Comment

by:sjgrey
ID: 35044658
Thanks

I put both subroutines in, one in the sheet and one in the workbook and it works

I'll experiemnet to see if I need both to make it happen but if you can tell me easily I'd be grateful

that's the only thing I can see is different from what I'd tried before but I may be mistaken

0
 
LVL 1

Author Closing Comment

by:sjgrey
ID: 35044668
There may have been something else going on as well but as soon as I inserted BOTH of the subroutines provided by TinTombStone it worked
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question