Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-11-2012, 03:41 AM
ibrahimaa ibrahimaa is offline Run Macro Windows Vista Run Macro Office 2007
Advanced Beginner
Run Macro
 
Join Date: May 2011
Posts: 35
ibrahimaa is on a distinguished road
Default Run Macro

I know that we can configure the macro to be run automatically once we open the xls file of by asking the user to click a specific button that trigger the macro.

However, I need to run the micro once the user enters a specific value in specific cell.

I heard about event macros and I do not know if it can solve this issue and how. I appreciate any help. Thank you.
Reply With Quote
  #2  
Old 05-11-2012, 07:59 PM
macropod's Avatar
macropod macropod is offline Run Macro Windows 7 64bit Run Macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi ibrahimaa,

You could use a 'Worksheet_SelectionChange' macro like the following in the relevant worksheet's code module:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Address = "$A$3" Then
  If Target.Value = 123 Then
    Application.EnableEvents = False
    'Call your other macro here
    Application.EnableEvents = True
  End If
End If
End Sub
In the above example, your macro will be called if cell A3 is selected and its value is 123. Note that merely selecting the cell when it has this value will be enough to trigger the macro. If you don't want that, you might prefer to use the 'Worksheet_Calculate' event. That event, though, won't be triggered by inputting anything into the cell unless that results in the worksheet re-calculating.
Code:
Private Sub Worksheet_Calculate()
If ActiveCell.Address = "$A$3" Then
  If ActiveCell.Value = 123 Then
    Application.EnableEvents = False
    'Call your other macro here
    Application.EnableEvents = True
  End If
End If
End Sub
You'll note that, in both case, I've included some Application.EnableEvents lines. Depending on what you're doing you may or may not need these.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-11-2012, 09:02 PM
Peter Wienand Peter Wienand is offline Run Macro Windows Vista Run Macro Office 2003
Novice
 
Join Date: May 2012
Posts: 15
Peter Wienand is on a distinguished road
Default

There is also the Worksheet_Change() function which will trigger when cells on the worksheet are changed by the user or by an external link. Note however that this event does not occur when cells change during a recalculation.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I assign a macro to a button when the macro is in my personal workbook? foolios Excel Programming 2 07-27-2011 02:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft