+ Reply to Thread
Results 1 to 14 of 14

Macro not working when called by IF formula

  1. #1
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Macro not working when called by IF formula

    Hello,

    I have a macro running in the background, monitoring for a value of 1 in a range of cells. I have two issues.

    1. If I type 1 into the top cell in the list, the macro executes as I want it to. If I type 1 in any of the cells below the top cell, it does not. - I get an out of range error, when it is not....

    2. I want the value of 1 in the cell range to be driven by the formula below, when I force one of the values in the referenced cells (E4 or K4) and make the IF statement return a 1, the macro does not execute.

    I have put the VBA and the IF statement below. Any help much appreciated !

    Thanks

    JM
    Please Login or Register  to view this content.
    =IF($O4=1,"Alert acknowledged",IF($AA4=1,"",IF(LEFT($E4,3) ="RSI",1,IF($K4<19.5,1,""))))

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 04-25-2017 at 11:04 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Macro not working when called by IF formula

    If you have a formula, you need to use the worksheet calculate event, not the change event. But to write code would depend on the specifics of what you want to do, which you don't explain fully. But try something like this:

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Many thanks for the example code. I can see from looking at it how it "should" work but the live monitoring feature does not work when I use this in isolation. - I tried to just set up from a blank workbook to test what was sent as well as to be able to send the example here if it failed.

    So as you will see, in range X4:X21, there are simple IF tests on cells in columns T&U. As soon as the value in either changes to make the formula in column X true and therefore return a value of 1, I want to activate the hyperlink in column W (for the same row as the alert / value was generated). When that hyperlink has been activated, I want to then put an override / marker in AA column and the same row for which the alert is generated to serve as a value used in a further IF statement to then check if the hyperlink has been activated.

    I hope that this makes a little more sense. - My primary issue with the new code which was sent is that it does not seem to monitor / be running in the background. - The previous code picked up whenever the value in column x changed to a 1 and launched the hyperlink.

    Many thanks for the help

    JM
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Macro not working when called by IF formula

    That code needs to go into the worksheet's codemodule, not a standard codemodule. Right-click the sheet tab, select "View Code" and the correct module will open in a new window.

  5. #5
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Hello,

    Many thanks for that. I have the code working in the simple sheet now.

    I am now stuck in a slightly different way. When I paste that code into the sheet which I have already with data links etc, after a few minutes, I end up with a run time error 13 = type mismatch. When I go to debug, the section of code highlighted as the issue is as below. I don't see where the issue is coming and am unclear as to how to trouble shoot it. When this error occurs, the sheet stops updating from the live datasource (Bloomberg) until I delete the code from the worksheet code module and reset the sheet.

    Please Login or Register  to view this content.
    Any ideas or pointers for how to fix this or further trouble shoot would be really useful.

    Thanks

    JM

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Macro not working when called by IF formula

    It might be getting into a loop - try it this way:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Thanks very much for the alternate way. There is good news and bad news.....

    Good is that it lasts longer before going to error (on the same line of code as before)

    Bad is that it still is doing the same thing.

    I REALLY appreciate the help but am not sure what additional information may be of use in trouble shooting this. - It is a 14mb file as it pulls and stores / references quite a lot of data.

    If there is another way to tackle the issue, then I can try that but I cant help but wonder if it could be some sort of setting in the sheet or the fact that it keeps getting refreshed from the external data source ?

    Many thanks

    JM

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Macro not working when called by IF formula

    Slight change in the code, with a change in the structure of the code, to see which part of that line is going wrong:

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 04-26-2017 at 01:14 PM.

  9. #9
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Hello Bernie,

    That seems to have done it ! - Just having the code split up into separate chunks has done the trick.

    Thanks so much !

    JM

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Macro not working when called by IF formula

    Actually, I think it was the error check - one or more of your formulas must error out at some point in the process, so the code checks that and skips them.

  11. #11
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Hi there,

    I seem to now be getting the error run time error 9. subscript out of range.....

    The piece of code which is highlighted when it breaks out is

    Please Login or Register  to view this content.
    I am re-stumped !

    Sorry but any ideas as to why this would be breaking out ? - The links in the column are clickable and work fine...

    Thanks

  12. #12
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Hello,

    I attach a slimmed down copy of the workbook which is causing me issues. I can construct a web address in column AE and it is clickable but as soon as I change the value in column W to reference the constructed website address, the VBA breaks out with runtime error 9. The code below works fine when the cell value in the referenced range is just text but when I refer to another cell (even using command =HYPERLINK(CELL REF), it breaks the VBA. The columns W, AA & AE are the main ones to look at.

    The section in bold below is where the error occurs, I don't see why changing the cell reference / contents should make it break like this. Does anyone have any ideas as to why this might be ?

    Thanks a lot

    JM

    Please Login or Register  to view this content.
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: Macro not working when called by IF formula

    Try replacing

    Please Login or Register  to view this content.
    with

    Please Login or Register  to view this content.
    Though you may need to change the formulas in W to show the actual hyperlink address instead of the display text

  14. #14
    Registered User
    Join Date
    04-19-2017
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    72

    Re: Macro not working when called by IF formula

    Thanks for the alternate suggestion.

    I have tried both just entering the text as well as using the =HYPERLINK() command and neither work. The error I now get is run time error 438. Object doesn't support this property or method.

    Sorry about this but are there any other possible ways to achieve this. - It just seems so odd that I can create a web address manually and click it myself but as soon as I use the reference to another cell / monitoring that it breaks out !

    Many thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. FindNext works when called from macro in debug but not from a cell formula
    By bambi42 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2016, 09:13 AM
  2. Sub not working when called
    By tigergutt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2016, 01:50 PM
  3. Macro not working properly when called from Button (Running otherwise)
    By AnimeshRoy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2016, 10:39 AM
  4. Last Sub not working when called
    By BGUNN in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-09-2015, 08:24 AM
  5. [SOLVED] Stopping a sorting formula called via macro from going to its target cell everytime
    By Ciais in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-07-2012, 03:38 AM
  6. Private sub not working correctly when called from another macro
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2008, 08:16 PM
  7. HELP macro function called from cell formula fails
    By mark00153 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2006, 03:32 PM

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.6.0 RC 1