ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how do you make a macro run automatically when a criteria is met? (https://www.excelbanter.com/excel-programming/367096-how-do-you-make-macro-run-automatically-when-criteria-met.html)

Lee Crew

how do you make a macro run automatically when a criteria is met?
 
I am trying to set up a marco that is run automatically if a certian word is
put into a certian cell in a workbook is this possible?

Many thanks
Lee

Tom Ogilvy

how do you make a macro run automatically when a criteria is met?
 
you could use the change event.

Right click on the sheet tab, select view code and paste in code like this
(adjusted for your actuall cell/word/macro to run)

Private Sub Worksheet_Change(ByVal Target As Range)
Static bFlag As Boolean
If Target.Count 1 Then Exit Sub
If Target.Address = "$B$9" Then
If Target.Value = "Goofy" Then
If Not bFlag Then
mymacro
bFlag = True
End If
Else
bFlag = False
End If
End If
End Sub

Chip Pearson's page on events:
http://www.cpearson.com/excel/events.htm

--
Regards,
Tom Ogilvy



"Lee Crew" wrote:

I am trying to set up a marco that is run automatically if a certian word is
put into a certian cell in a workbook is this possible?

Many thanks
Lee


halim

how do you make a macro run automatically when a criteria is met?
 
Hi Tom,

I'm just a beginner in this stuff
why don't use smple way like :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target = "My word" Then My Macro
End Sub

Just asking ...
Thanks,

Halim


Tom Ogilvy wrote:
you could use the change event.

Right click on the sheet tab, select view code and paste in code like this
(adjusted for your actuall cell/word/macro to run)

Private Sub Worksheet_Change(ByVal Target As Range)
Static bFlag As Boolean
If Target.Count 1 Then Exit Sub
If Target.Address = "$B$9" Then
If Target.Value = "Goofy" Then
If Not bFlag Then
mymacro
bFlag = True
End If
Else
bFlag = False
End If
End If
End Sub

Chip Pearson's page on events:
http://www.cpearson.com/excel/events.htm

--
Regards,
Tom Ogilvy



"Lee Crew" wrote:

I am trying to set up a marco that is run automatically if a certian word is
put into a certian cell in a workbook is this possible?

Many thanks
Lee



Tom Ogilvy

how do you make a macro run automatically when a criteria is m
 
SelectionChange fires when the cell is selected. If the user entered My
word, the macro would not be fired until the user reselected the cell. Then,
after it had fired, it would fire again each time the user selected the cell
(and its value remained My word) and it would fire everytime the user
selected any cell that contained My word. If the user selected several cells
your code would raise a type mismatch error. So the smple way you suggested
doesn't answer the requested functionality and is flawed.

--
Regards,
Tom Ogilvy


"halim" wrote:

Hi Tom,

I'm just a beginner in this stuff
why don't use smple way like :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target = "My word" Then My Macro
End Sub

Just asking ...
Thanks,

Halim


Tom Ogilvy wrote:
you could use the change event.

Right click on the sheet tab, select view code and paste in code like this
(adjusted for your actuall cell/word/macro to run)

Private Sub Worksheet_Change(ByVal Target As Range)
Static bFlag As Boolean
If Target.Count 1 Then Exit Sub
If Target.Address = "$B$9" Then
If Target.Value = "Goofy" Then
If Not bFlag Then
mymacro
bFlag = True
End If
Else
bFlag = False
End If
End If
End Sub

Chip Pearson's page on events:
http://www.cpearson.com/excel/events.htm

--
Regards,
Tom Ogilvy



"Lee Crew" wrote:

I am trying to set up a marco that is run automatically if a certian word is
put into a certian cell in a workbook is this possible?

Many thanks
Lee





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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com