+ Reply to Thread
Results 1 to 4 of 4

Thread: Macro won't run when using target cell is changed via "Calculation"

  1. #1
    Registered User
    Join Date
    08-30-2010
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2003
    Posts
    1

    Smile Macro won't run when using target cell is changed via "Calculation"

    Hallo

    I have a spreadsheet that gets updated by a 3rd party application via DDE. That 3rd party application populates various cells. I then have a macro that must run when one of the target cells is changed. I have tried:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$O$2" Then
    Call myMacro
    End If
    End Sub
    BUT, the problem is that this only works when the user changes the "O2" cell manually, and it appears as if the worksheet sees the DDE updating as a formula calculation.

    The "Private Sub Worksheet_Change(ByVal Target As Range)" statement above will not work when the values are automatically updated in "O2" via DDE.

    What code will work that will run "myMacro" regardless of whether "O2" gets changes manually or via DDE?

    If you want to help, please comment with actual code examples; I am not a programmer, so general comments like sending me to this page or that page or "try this function" etc, are not really going to help. Please help, and thanks in advance!

    PS Sorry for the edits required, but should be clear now, and I understand the rules going ahead
    Last edited by gravity1000; 08-30-2010 at 11:17 AM. Reason: Incorrect title and no codes

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Run a macro, help!

    Welcome to the forum.

    Please take a few minutes to read the forum rules, and then edit your post to amend the thread title accordingly and add CODE tags.

    Thanks.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: Macro won't run when using target cell is changed via "Calculation"

    Use the calculate event, rather than the change event. Unfortunately this will cause the macro to run everytime the sheet calculates, so your myMacro should be as lean as possible.

    Also, you need to add a condition to see if myMacro should run, because the Calculate event isn't target specific. So, you need to have something like "If Range("O2") = ..."

    Private Sub Worksheet_Calculate()
        If Range("$O$2") = 1 Then
            Call myMacro
        End If
    End Sub
    If you want to compare if O2 has changed, and then run myMacro regardless of O2's value, then you would need to have a helper cell somewhere in another worksheet or some out-of-the-way cell. I usually create a worksheet called "System" where I keep all the helper data that will just clutter your presentation.

    So, something like this would work:

    Private Sub Worksheet_Calculate()
        If Range("O2") <> Sheets("System").Range("A1") Then
            Call myMacro
            Sheets("System").Range("A1") = Range("O2")
        End If
    End Sub

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Macro won't run when using target cell is changed via "Calculation"

    I have two solutions, and have tried neither. The first is from Colin Legg:

    A DDE Link update will not raise the worksheet's Change event. A good way to deal with a few DDE links is to use an ActiveX textbox. Add one to the worksheet and then, in the Properties window, assign the appropriate cell address to its LinkedCell property. In the worksheet class module you can then add a textbox change event handler that runs each time the value in the cell changes:
    Private Sub TextBox1_Change()
         ' process here
    End Sub
    The textbox can then be hidden by setting its Visible property to False. The advantage of this approach is that it specifically reacts to the single cell updating, rather than the Calculation event raised by when any cell is changed.
    The second is adapted from one I saw from Microsoft:
    Sub SetDDEProcess()
        Dim avLink       As Variant
        Dim i           As Long
        Dim bHasDDE     As Boolean
    
        With ActiveWorkbook
            avLink = .LinkSources(xlOLELinks)
            
            If Not IsEmpty(avLink) Then
                For i = 1 To UBound(avLink)
                    If avLink(i) Like "*|*!*" Then ' it's a DDE link
                        .SetLinkOnData avLink(i), "LinkChange"
                        bHasDDE = True
                    End If
                Next i
            Else
                MsgBox "No OLE or DDE links!"
            End If
        End With
            
        If Not bHasDDE Then MsgBox "No DDE links!"
    End Sub
    
    Sub LinkChange()
        ' process here
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0