+ Reply to Thread
Results 1 to 17 of 17

Code doesn't run when cell contain formula

  1. #1
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Code doesn't run when cell contain formula

    Good day Excel Gurus.

    I'm trying to figure out, why is that, when any cell in column A contain formula, the code does not run as it should?
    For example C3 failed to return the value from Database (or not found), because A3 contains formula.
    But it is working normally when there's no formula in it.
    Please help. Thanks!
    Attached Files Attached Files
    Last edited by kenjcd; 07-18-2022 at 09:01 AM.

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Code doesn't run when cell contain formula

    Worksheet_Change trigger only manual change only, not change from formula in A3. Try vlookup A3 instead.
    Quang PT

  3. #3
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    Thanks for the prompt reply.
    If I do vlookup without removing the code, it will return without the comment along.
    If I do vlookup with the code intact, the formula will be removed once it return the value.

    Any workaround the code if possible?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Code doesn't run when cell contain formula

    I'm not sure I understand exactly how you are interacting with the spreadsheet, but I am wondering if, rather than a change event, you are looking for something triggered by a calculate event, or maybe some other event.

    It might a bit much, but I would suggest you explain exactly what "it works normally when there's no formula in the cell" and exactly what you mean by "when a cell contains a formula, it doesn't work as it should." Once we understand exactly how you are interacting with the sheet and what "works" and "does not work" mean, then maybe we can help by suggesting a better event structure or how to change the existing change event code.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    The code is doing exactly what I wanted to. But when I try to put a formula in A3 (sheet L1)
    =if(e3="max",1,if(e3="min",2)) by right, the value in C3 (sheet L1) should lookup A3 (sheet L1) and return the corresponding value from Database like a normal vlookup.

    I need this code, because so far, it is able to return a vlookup along with comment (text and picture).

    Unfortunately, if I put a formula column A (any cell), it would disrupt the code?
    I'm so sorry if my explanation if confusing, but could you please take a look a the sample sheet.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Code doesn't run when cell contain formula

    I did look at the sample sheet. What I see is a change event procedure that is specifically looking for changes/edits to column A (the "If cell.Column=1 Then...End If" block if). When you talk about putting a formula into column A, that suggests that, as the project develops, you no longer intend to change/edit column A. As currently programmed, you have pushed the changes/edits into column E (column 5). Assuming you still want to use a change event, then you would need to change the code to check for changes in column 4 rather than column 1. But, the sample sheet appears to be far from complete, so I am wondering if the next phase of the project will be to add a formula to column E, indicating that you are not going to change/edit column E in the final version of the spreadsheet.

    Ultimately, the end goal is to identify which cells are going to be your main input cells that you will be changing/editing when the spreadsheet is completed so you can program the change event procedure to monitor those cells and respond appropriately. If that will be column E, the changes to the event procedure should be straightforward. Rather than make those changes now, I would wait until the spreadsheet is closer to its final form where we are more certain which cells will be the main input/edit/change cells, then visit the change event procedure to make it responsive to those input cells.

  7. #7
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    Good day MrShorty
    Thanks for your feedback. The spreadsheet is almost in its final form. The only thing that is delaying me from completing it is because of is this.

    I have uploaded another worksheet for you to look into.
    To clarify, the main input cell is going to be Main!E2
    Once input value is keyed in Main!E2, any cell in column A, should be able to accept formula and run the code.
    I hope this explanation make sense to you. Thanks ya.
    Attached Files Attached Files
    Last edited by kenjcd; 07-15-2022 at 06:52 AM.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Code doesn't run when cell contain formula

    I don't use event code in my spreadsheets, so I'm not that skilled at using them. My first question, where this change event will be looking for changes to a cell on one sheet and changes to that cell will trigger code to change other sheets in the workbook, will this be better as a workbook_change event procedure (in the ThisWorkbook module) or will this be better as a sheet_change event in the Main module? I would have to rely on those with more experience than me to answer that question, or rely on you to decide which you would prefer.

    In either the case, the main flow of the procedure is to watch for changes in the worksheet/workbook and when Main!E2 is changed/edited, then go to L1 and do the Find, Copy, Paste operation from Database into L1. The main change I see to this part of the current procedure is changing the Offset() methods/properties so that they are no longer relative to the change event's target cell, but always referencing column C of L1.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Code doesn't run when cell contain formula

    … where this change event will be looking for changes to a cell on one sheet and changes to that cell will trigger code to change other sheets in the workbook, will this be better as a workbook_change event procedure (in the ThisWorkbook module) or will this be better as a sheet_change event in the Main module?
    Not really. If, let's say, you have 10 sheets all laid out the same, doing the same thing, and reacting to the same events, you could use a Workbook Sheet Change Event handler rather than repeat the code 10 times. You'd the use the sh variable to determine which sheet has been changed. Where you have a change to one sheet, whether or not it affects other sheets, you would just have the code behind that sheet. If the change affects other sheets, it may be appropriate to disable event handling to avoid cascading changes … although that may be what you want to achieve. Hope that makes sense. What you are trying to avoid is one change initiating unnecessary changes and potentially looping.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  10. #10
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Code doesn't run when cell contain formula

    If you remove the code from the L1 Sheet module, and place this code in Main sheet tab

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    Hi davesexcel
    1. I have put this under MAIN
    Please Login or Register  to view this content.
    And this as module
    Please Login or Register  to view this content.
    Result is, once I run the macro, it will return value without the comment attached.
    Please advise.

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Code doesn't run when cell contain formula

    It seems to me that all you need to do is change the block If where the cell value is copied to something uses a copy/paste method as you had done in your original code. As I noted above, the offsets will now need to be relative to column A rather than the changed cell. So replace this
    Please Login or Register  to view this content.
    with something like
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    L1!A2 and L1!A3 is where your solution applied.
    It is possible for your code to do like L1!A6?
    Attached Files Attached Files

  14. #14
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Code doesn't run when cell contain formula

    Try this,
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    Dave, just a little bit more, is it possible for the comments to 'refreshed' accordingly?

  16. #16
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Code doesn't run when cell contain formula

    There should be two worksheet_change events, one for "L1", other for "Main"
    For "Main":
    PHP Code: 
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As RangevFND As RangeL1 As WorksheetData As Worksheet
    If Target.Address <> "$E$2" Then Exit Sub
    Set L1 
    Worksheets("L1"): Set Data Worksheets("Database")
    For 
    Each cell In L1.Columns("A").SpecialCells(xlCellTypeFormulas)
        
    Set vFND Data.Range("A:A").Find(cellLookIn:=xlValuesLookAt:=xlWhole)
        If 
    Not vFND Is Nothing Then
            vFND
    .Offset(, 1).Copy cell.Offset(, 2)
        Else
            
    cell.Offset(, 2) = "not found"
        
    End If
    Next
    End Sub 
    For "L1"
    PHP Code: 
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As RangevFND As RangeL1 As WorksheetData As Worksheet
    If Target.Column <> Or Target.Count 1 Then Exit Sub
    On Error Resume Next
    Set Data 
    Worksheets("Database")
    Set vFND Data.Range("A:A").Find(TargetLookIn:=xlValuesLookAt:=xlWhole)
        If 
    Not vFND Is Nothing Then
            vFND
    .Offset(, 1).Copy Target.Offset(, 2)
        Else
            
    cell.Offset(, 2) = "not found"
        
    End If
    End Sub 
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    08-22-2010
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Code doesn't run when cell contain formula

    Thank you so much davesexcel and bebo021999.
    Thanks for helping me out. Stay safe!
    Rep added.

+ 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. VBA code to follow hyperlink doesn't work when cell contains formula
    By PFDave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-26-2016, 04:19 AM
  2. [SOLVED] Add string as formula through code doesn't work with sumif
    By vizzkid in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-23-2016, 10:04 AM
  3. Formula doesn't work when run as code in a macro
    By gers1978 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-02-2015, 12:45 PM
  4. Code to Delete Row if a cell in the Row doesn’t begin with a certain value/s
    By Motox in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2014, 12:49 AM
  5. why doesn't code reset cell color back?
    By rgouette in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2014, 11:32 AM
  6. VBA code doesn't work when formula in sheet is removed
    By Marco-Kun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2009, 01:18 PM
  7. Code doesn't update from a cell?
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-28-2007, 11:40 AM

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