2

What I am needing: A macro to be triggered, let's call the macro "MacroRuns", whenever cell C3 returns a different value than it currently has, based on its FORMULA, NOT based on manually typing a different value.

I have spent all day reading through and attempting every "solution" on the first two pages of my google search on this topic. So far, nothing seems to work for me. Please help!!! I would very much appreciate it!

Example:

I have now tried this but it corrupts my file after it works a few times.

Private Sub Worksheet_Calculate()
    If Range("E3") <> Range("C3").Value Then
        Range("E3") = Range("B3").Value
        MsgBox "Successful"
    End If
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
soundship
  • 33
  • 1
  • 1
  • 6
  • 4
    You would use Worksheet_Calculate Event – Scott Craner Jan 17 '19 at 19:43
  • Thanks Scott, but whenever I use solutions with Worksheet_Calculate, my code is run every time the same same or different sheet's cells are accessed and enter is pressed. I have seen in other posts that because I am using an indirect formula in my workbook, it causes Worksheet_Calculate to operate in this way. – soundship Jan 17 '19 at 19:57
  • yes you will need to save the value some where and check the new value against the old, if different then run the code and update the cell in which you saved the value, priming it for the next test. – Scott Craner Jan 17 '19 at 19:58
  • I have read several solutions like this, so I agree, but for some reason I cannot get any of them to work. Do you have an example of code I could try? Perhaps I'm missing something when I copy paste their solutions and alter to meet my need.. – soundship Jan 17 '19 at 20:01
  • If I put some test code, it will probably have the same issue as the ones you copied, pasted and changed. On this site the person posing the question shows their code and explains the errors that are being recieved, not the other way around. – Scott Craner Jan 17 '19 at 20:02
  • That makes sense. I am currently trying this: `Private Sub Worksheet_Change(ByVal target As Range) If Not Intersect(target.Address, Range("C3:C4")) Is Nothing Then MsgBox "I ran" End If End Sub` -- I get a "Compile Error - Type Mismatch" on the line target.address – soundship Jan 17 '19 at 20:06
  • Worksheet Change will not pickup the change due to formula, please show how you tried to save the value and compare in the Worksheet_Calculate event. – Scott Craner Jan 17 '19 at 20:08
  • And do not post clarifications to the question in the comments, instead [edit] your original question with the code/clarifications – Scott Craner Jan 17 '19 at 20:09
  • my guess as to why the current code is not running is that you have `target` as global variable or a sub name or a function name. The fact that it is lower case indicates that you have used it in that manner somewhere and it is taking president over the natural state. – Scott Craner Jan 17 '19 at 20:18

3 Answers3

3

Module1, Sheet1 (Calculate), ThisWorkbook (Open)

Highlights

  • When the workbook opens, the value from C3 is read into the public variable TargetValue via TargetStart.
  • When the value in C3 is being calculated, TargetCalc is activated via the calculate event.If the current value in C3 is different than TargetValue, MacroRuns is triggered and TargetValue is updated with the value in C3.

The Code

Module1

Option Explicit

Public TargetValue As Variant
Private Const cTarget As String = "C3"

Sub TargetCalc(ws as Worksheet)
    If ws.Range(cTarget) <> TargetValue Then
        MacroRuns
        TargetValue = ws.Range(cTarget).Value
    End If
End Sub

Sub TargetStart()
    TargetValue = Sheet1.Range(cTarget).Value
End Sub

Sub MacroRuns()
    MsgBox "MacroRuns"
End Sub

ThisWorkbook

Option Explicit

Private Sub Workbook_Open()
    TargetStart
End Sub

Sheet1

Option Explicit

Private Sub Worksheet_Calculate()
    TargetCalc Me
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • Thank you so much for your time! I will try this right now. – soundship Jan 17 '19 at 20:32
  • The "C3" that I need it to remember is on a hidden sheet called "CALC_CompStatus". How do I add that to the code you provided above? – soundship Jan 17 '19 at 20:34
  • @soundship: I don't know what you're asking. cTarget is a string that contains "C3", it is not a range. TargetCalc is used in any sheet where you want it (don't know about hidden) and TargetStart is used in Sheet1. So you have to adjust Sheet1 and put the code for Sheet1 to your actual sheet code. – VBasic2008 Jan 17 '19 at 20:41
  • 1
    @VBasic2008 `Range(cTarget)` is going to refer to the active sheet and not the sheet that calls it. You may want to pass the worksheet into the sub: `Sub TargetCalc(ws as Worksheet)` then you can append the range object with the correct parent: `ws.Range(cTarget)` and you would call it `TargetCalc me` – Scott Craner Jan 17 '19 at 20:46
  • Thank you both. @ScottCraner, It works flawlessly from another sheet, even when the sheet with C3 is hidden. Thank you! – soundship Jan 17 '19 at 21:05
  • This would work consistently until I replaced the MsgBox with Call MyMacro. For some reason, it would then continually run my macro and freeze up the file, making it a corrupt file that needed repairing each time I opened it. Oddly enough, when I tried the code below by @Ferdinando, I was able to plugin and run my macro without any problems. I'm sure the issue is just on my part and lack of knowledge as to what was causing this, but I really appreciate your help Scott. – soundship Jan 18 '19 at 00:27
  • After further testing, the code used from Ferdinando would work the majority of the time, but sometimes it would not. I tried implementing your code once more and have been testing throughout the day and it works great, apart from the looping issue. – soundship Jan 18 '19 at 18:15
  • @ScottCraner I made need to post another question for this one, but now I need to tell your code to stop running my code "MacroRuns" after MacroRuns has run 1 time. I am currently using a MsgBox to ask if I'd like to run it, giving me a way to stop it from looping, but any suggestions would be appreciated. – soundship Jan 18 '19 at 18:17
  • well thank you for your help. I'll post another question for this. – soundship Jan 18 '19 at 18:21
0

If i understood your question you can try this code:

1)Right-click the Sheet tab and then click View Code

  1. copy this code:

    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Value1 As Variant Static Value2 As Variant

    Value1 = Range("C3").value

    If Value1 <> Value2 Then MsgBox "Cell has changed." End If

    Value2 = Range("C3").value

    End Sub

i tried this one:

in cell C3 i have wrote =SUM(A1:B1) when i try to change value in this cells also C3 change and i get the msgBox

Hope this helps

EDIT the code to answer @ MD Ismail Hosen

if i understood your problem you can try this example code:

Private Sub Worksheet_Change(ByVal Target As Range)

'in this code i have used two range on the same row, but you can change as 
'you want. 
'In my case, the range that i check is Range("A1:C1") and the RANGE that i 'save old value is 
'RANGE("F1:H1") F1 is the sixth column.

Dim counter As Byte
Dim sizeRange As Byte

sizeRange = 3 ' my size range

For counter = 1 To sizeRange
    'on the left i check Range("A1:C1").On the right i check The Range("F1:H1")
    If Cells(1, counter) <> Cells(1, counter + 5) Then 'counter start from 1 
        MsgBox "Range Changed"
        Range("A1:C1").Copy Destination:=Range("F1:H1") ' use other code to copy the range
        Exit For
    End If
Next counter
End Sub

If you have a formula in your range ("A1:C1") you have to use this code to copy the new range value A1:C1 in F1:H1 else you get the error(loop the macro). 'TO use this code if you have formula in the cells.

 Range("A1:C1").Select
        Selection.Copy
        Range("F1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Hope this helps.

Ferdinando
  • 964
  • 1
  • 12
  • 23
  • Thank you so much @Ferdinando! This did it for me. So far I have not had any issues and this works as I'd hoped. – soundship Jan 18 '19 at 00:29
  • This doesnt answer the question. He is talking about when result of a formula changes rather than a change that would trigger this routine. Such as a manual cell value change by the user. He isnt talking about that. – Gary Carlyle Cook Dec 27 '19 at 04:17
  • Dear @Gary Carlyle Cook, i think that my answer is good because in soundhip's post he says that works...no? – Ferdinando Dec 28 '19 at 09:50
  • @Ferdinando what if there has a range(B10:B64)..Is it going to work..? – MD Ismail Hosen Sep 25 '20 at 20:28
  • Yes, @MD Ismail Hosen, try It. In Cell C3 you put this new range(B10:B64). – Ferdinando Sep 27 '20 at 08:40
  • In case of variant i need to loop through each item, right? – MD Ismail Hosen Sep 27 '20 at 13:56
  • Sorry @MD Ismail Hosen,but did you try the code? The answers are in the code...if you have a problem post your issue here...and i Hope help you... – Ferdinando Sep 28 '20 at 15:06
  • I have tried it but it does not work in my case i need to call another sub from this event..So instead of static variable i store the data in a range and compare both the range..Thanks – MD Ismail Hosen Sep 28 '20 at 15:49
  • @MD Ismail Hosen, I have edited my code in the answer. I hope this help you – Ferdinando Sep 29 '20 at 21:35
  • @Ferdinando I use the same procedure for comparing values...Thanks..My data was in a column...It worked...I thought that may be there was a way to compare in one line instead of loop... – MD Ismail Hosen Sep 29 '20 at 21:43
  • Nice @MDIsmailHosen, if for you there is not a problem you can vote my answer. Always if you want. – Ferdinando Sep 29 '20 at 21:45
0

Right. I have a nugget to add in here, something that completely frustrated me upon trying Ferdinando's code (which by itself is very neat, thank you, Ferdinando!!)

The main point is - if you are going to be using anything beyond just a messagebox (MsgBox "Cell has changed.") you need to add the following lines above AND below this line(otherwise the Excel will simply crash constantly due to endlessly trying to do the same). Don't ask me why this is, but I finally-finally solved my problem with this. So here are the lines:

If Value1 <> Value2 Then
(ADD THIS:)     Application.EnableEvents = False
                MsgBox "Cell has changed."
(I call a macro running a query from MySQL instead of MsgBox)
(AND ADD THIS:) Application.EnableEvents = True

Hope this helps anyone in the situation I was in!!