How can I automatically execute a macro if there a 1 or -1 in a cell

arrautxa

New Member
Joined
May 6, 2009
Messages
8
I have the following problem.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I do have a worksheet where I have several functions. One of those sub is as follows:<o:p></o:p>
<o:p></o:p>
Private Sub Worksheet_Change(ByVal Target As Excel.Range)<o:p></o:p>
If Target.Column = 1 Then
If Target.Row = 6 Then
If Target.Value = 1 Then
'Call macro here
End If
End If
End If<o:p></o:p>

<o:p></o:p>
if I place manually 1 in cell A,6 and manually click enter, it will call the macro with no problem.<o:p></o:p>
What I need help with is that I am planning to place a 1 or -1 on that cell automatically which is sent by another macro but I need help is to check if the is a 1 with the private sub above and need to perform the click automatically without my intervention. Once the macro is confirmed to be executed I would like to blank that cell in order to wait for the next instruction.
A,6 will not be the only cell that will have to check for a 1 but the check will always be on column A.
Can anybody help me with this macro.
Best Regards
Bill
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hello Andrew. Thanks for responding so soon. (3AM wao)
I am using
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

I have a simplistic code that when there is a particular number lets say 1 or -1 in a particular cell and I press enter it fires the macro no problem.What I don't know how to do it is when I have a formula on that particular cell and generate a value, Worksheet_Change does not seems to recognize it as a value but instead as a formula and don't execute anything.

I try Worksheet_Calculate but it enter in a loop because other changes occuring in other worksheet on the same workbook.I didnt know how to use that function for a specific worksheet or a particular range of cells.(is it possible?)
Could you help me . Thanks in advance for your great help. I already PM with the question and the code I am using.
Regards
Bill
 
Upvote 0
The Worksheet_Calculate event procedure doesn't have a target argument. If you want to avoid it being called repeatedly put:

Application.EnableEvents = False

at the beginning of the procedure and:

Application.EnableEvents = True

at the end.

Maybe you can use the cell's precedent cell in the Worksheet_Change event procedure. What's the formula that it contains?
 
Upvote 0
First of all Thanks Andrews for your help.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
With you suggestion I look up Internet and found the following: Bellow in red<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
What I am trying to accomplish is: If there is a formula on a range lets say c5:c300 either or in several non contiguous cells in column A, and the result of that formula<o:p></o:p>
Is a 1 or -1 a 1 or -1 will have to be placed on column A. <o:p></o:p>
When there is a 1 /-1 in column A, that action will trigger a sub called INSMKTGU if it is 1 and INSCLOSEMKTGU if it -1<o:p></o:p>
Then once the sub has been triggered I need to clear column A.<o:p></o:p>
<o:p></o:p>
The problem: <o:p></o:p>
At this moment the way I am doing it is working but neither efficiently nor correctly. Whenever there is a 1 product of the formula in column C, a 1 or -1 will appear on A (that is what I am expecting to happen).<o:p></o:p>
But instead of triggering sub INSMKTGU once it triggered several times.
Following the result of the formula on column C will be 0 so I clear the content on A. This clearing takes to much time and I believe is because the loop is to long.<o:p></o:p>
Could you please help me to restrict INSMKTGU or INSCLOSEMKTGU for being triggered more than one at a time and to clear the content on column A once either subs has been triggered.<o:p></o:p>
I will PM with the excel worksheet so you don't have to replicate what I am writing above.
Thanks a lot in advance.<o:p></o:p>
Bill<o:p></o:p>
<o:p></o:p>
Private Sub Worksheet_Change(ByVal Target As Excel.Range)<o:p></o:p>
Dim keyCells As Range<o:p></o:p>
<o:p></o:p>
Set keyCells = Range("c5"): Rem cell With formula<o:p></o:p>
<o:p></o:p>
On Error Resume Next<o:p></o:p>
Set keyCells = Application.Union(keyCells, keyCells.Precedents)<o:p></o:p>
On Error GoTo 0<o:p></o:p>
<o:p></o:p>
If Not Application.Intersect(Target, keyCells) Is Nothing Then<o:p></o:p>
MsgBox "Cell c5, containing a formula, has been changed."<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
If Target.Value = 1 Then<o:p></o:p>
change'This is the sub that clear content on column A and place the result of the formula en C in Column A.<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
If Target.Value <> 0 Then<o:p></o:p>
change'This is the sub that clear content on column A and place the result of the formula en C in Column A.<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
If Target.Value > 1 Then<o:p></o:p>
change'This is the sub that clear content on column A and place the result of the formula en C in Column A.<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
'the following will trigger when there is a 1 or -1 in column A<o:p></o:p>
<o:p></o:p>
If Target.Column = 1 Then<o:p></o:p>
If Target.Row = 5 Then<o:p></o:p>
If Target.Value = 1 Then<o:p></o:p>
INSBUYMKTGU<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
If Target.Column = 1 Then<o:p></o:p>
If Target.Row = 5 Then<o:p></o:p>
If Target.Value = -1 Then<o:p></o:p>
INSCLOSEBUYMKTGU<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Private Sub change()<o:p></o:p>
'Private Sub Worksheet_Calculate()<o:p></o:p>
'Dim lRtn As Long<o:p></o:p>
'lRtn = OrderForm.fctGetMainBidAsk()<o:p></o:p>
<o:p></o:p>
Dim i As Long, Srow As Long, ERow As Long, Intrvl As Long<o:p></o:p>
Srow = 5 'Starting Row<o:p></o:p>
ERow = 350 'Ending Row<o:p></o:p>
Intrvl = 1 'Interval<o:p></o:p>
<o:p></o:p>
For i = Srow To ERow Step Intrvl<o:p></o:p>
Select Case Range("c" & i).Value<o:p></o:p>
'Case 1<o:p></o:p>
'Range("A" & i).ClearContents<o:p></o:p>
'Case ""<o:p></o:p>
'Range("A" & i).ClearContents<o:p></o:p>
'Range("A" & i).Value = 0<o:p></o:p>
Case 0<o:p></o:p>
Range("A" & i).ClearContents<o:p></o:p>
'Range("A" & i).Value = 0<o:p></o:p>
Case Is > 0<o:p></o:p>
If Len(Range("A" & i)) >= 0 Or Len(Range("A" & i)) = "" Or Len(Range("A" & i)) < 0 Then<o:p></o:p>
Range("A" & i).ClearContents<o:p></o:p>
'Range("A" & i).Value = 0<o:p></o:p>
Range("A" & i).Value = Range("B" & i).Value<o:p></o:p>
End If<o:p></o:p>
Case Is = -1<o:p></o:p>
If Len(Range("A" & i)) <= 0 Or Len(Range("A" & i)) = "" Or Len(Range("A" & i)) > 0 Then<o:p></o:p>
'Range("A" & i).ClearContents<o:p></o:p>
'Range("A" & i).Value = 0<o:p></o:p>
Range("A" & i).Value = Range("B" & i).Value<o:p></o:p>
End If<o:p></o:p>
End Select<o:p></o:p>
Next i<o:p></o:p>
'End Sub<o:p></o:p>
End Sub
 
Upvote 0
Yes sir I did what you suggested on post #4 and it partially works because seems to put a 1 or -1 only one time but now when a 1 or -1 appear on cell 5.1 do not trigger the sub that suppose to call but if you manually write a 1 or -1 on that cell and hit enter the sub is called as it suppose to do it.
Thanks in advance
Bill
 
Upvote 0
Andrew is there any way that I could send you PM with the workwook so you could evaluate what is the problem??
Thanks in advance
Bill
 
Upvote 0
Re: how can I trigger a macro when a range of cells in workbook XX are link to range of cells in another workbook YY

Andrew This similar of what I posted above but now I would like to know
if you could help me with this:
how can I trigger a macro when a range of cells in workbook XX are link to range of cells in another workbook YY and either of these cells changes from 0 to either -1 or +1 without me interacting.

Depending which cell in sheetXX within the specify range in workbook XX changes it will have to trigger a particular macro.

On Sheetyy in Workbook YY there are going to be series of calculations.Depending on those calculations there will be a 1 or -1 in column A Since this column range is linked to SheetXX in WorkbookXX
whenever there is a change in any of the cells it will trigger the macro that correspond.
change in cell A5 will execute macro1
change in cell A6 will execute macro 2
change in cell A9 will execute macro 3
change in cell A10 will execute macro 4
and so on.

The code in earlier post kind of work if I click enter the code bellow king of work if I click on thenworkbook Ineed help trigering different macros without me intervening.

I am trying the following but it seems that because of Worksheet_Calculate() which aparentlyis the one detecting the change and triggering.You will see the workbook flashing and will not show the message until I click on the workbook.
The problem with this approach is that if I have more calculations or changes going on in other worksheet in the same workbook it will enter in a loop.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
If Range("A1").Value = 1 Then
'MyA2
'MyA2 = Range("A1").Value
MsgBox "Hello - A1 changed"
End If
'End If

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

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then

If Range("A1") = 1 Then
MsgBox "Cell " & Target.Address & " has changed."
End If
End If


End Sub
Private Sub Worksheet_Calculate()
If Range("A2").Value = 2 Then
'MyA2
MyA2 = Range("A2").Value
MsgBox "Hello - A2 changed"
End If
If Range("A1").Value = 1 Then
'MyA2
MyA2 = Range("A1").Value
MsgBox "Hello - A1 changed"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top