+ Reply to Thread
Results 1 to 17 of 17

Clear cell contents from a function

  1. #1
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Clear cell contents from a function

    I understand you can't change value of a cell from within a function. ie. =Function(B12)

    If the value in B12 changes to a 1 then I want the VBA code to clear the contents of this particular cell.

    Range("B12").ClearContents works great from a Sub but does not from within the Function code.

    Any ideas?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Clear cell contents from a function

    Hello Chris,

    Can you provide an example of what you to do?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    This is a simplified version of the user defined function. In cell B13 I have =Monitor(B12) so if B12 changes value it will run the function. It displays the msgbox no problem but I cannot get it to clear the contents of B12. Works fine if I wrap Range("B12").ClearContents in a Sub.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 03-22-2012 at 02:51 PM. Reason: Added Code Tags

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Clear cell contents from a function

    Hello Chris,

    If you are planning to use this as User Defined Function (UDF) for the worksheet. It will not work. There are restrictions on what you can and can not do with a UDF. Have a look at the links below to see the restrictions.

    Description of limitations of custom functions in Excel
    Excel User-Defined Functions

  5. #5
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    what would you recommend I do?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Clear cell contents from a function

    Hello Chris,

    If I knew more about what you were doing then I could give you some direction or a possible workaround. Can you post the workbook for review?

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Clear cell contents from a function

    @ Leith Ross,

    Hello Leith,

    Nice to see you so active and ever helping. I am looking forward to the final outcome of this Thread.

  8. #8
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    Hi Leith, apologies for the delay. I have attached a simple example of what I am trying to achieve. Hopefully you can figure out a workaround for this issue. Appreciation in advance.
    Attached Files Attached Files

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Clear cell contents from a function

    Hello chris1983,


    ClearContents works great from a Sub but does not from within the Function code

    Then why don't you just add this code to Sheet1?
    Please Login or Register  to view this content.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  10. #10
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    Thanks for responding Winon, I have more complicated VBA that watches 1000 lines, reads the values of the column into an array and compares any new values (changes) to the existing values in the array. So this NEEDS to be a function unfortunately.

  11. #11
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Clear cell contents from a function

    Hello chris1983,

    Thank you for responding,

    May I suggest you then upload a revised version of your WorkBook reflecting your exact scenario, so that we can see how to "uncomplicate" things a bit.

    If I cannot help you, I would be more than willing to surrender to Leith Ross, he is, in my opinion outstanding!

  12. #12
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    I see your point but I feel my code would just muddy the water, so I would just rather keep it simple: I need to clearcontents of a cell from a Function. It has to be from a function and if it can't be achieved then at least I have got a second opinion.

  13. #13
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Clear cell contents from a function

    To do something like this, you need a combination of UDF and event code.
    Put something like this in a normal module

    Please Login or Register  to view this content.
    Then in the ThisWorkbook code module
    Please Login or Register  to view this content.

    The idea is that:
    1) a UDF cannot clear a cell when called from a worksheet.
    2) a UDF can put a range into a collection, even when called from a worksheet
    3) the Calculate event runs after all UDF's are calculated
    4) the Calculate event can clear cells

    The UDF, ClearIfOne, puts the appropriate cells into a collection. The contents of that collection are later cleared by the Calculate event. (I prefer to use the Calculate event in ThisWorkbook since I don't want the UDF to be restricted to use in only one sheet.)

    You can adapt this approach if you want ClearIfOne to return something other than TRUE/False.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  14. #14
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    Thnx Mike, it seems to have issues populating the CellsToClear collection with line:
    CellsToClear.Add Item:=aCell, Key:=aCell.Address(, , , True)

    With this line it doesn't return TRUE but if I comment it out it does. Can you shed any light on this?

  15. #15
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Clear cell contents from a function

    What happens when you step through the code.
    If aCell is already in the collection, there will be an error and the UDF will return False.
    But then the Calculation event should run and the Set CellsToClear to Nothing, emptying it.

    Code like this can be tricky (it may be nessesary to turn off calculation during the Calculate event) and I'm working from memory.

    When I get off work, I'll put together a test situation and debug.

  16. #16
    Registered User
    Join Date
    03-07-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    55

    Re: Clear cell contents from a function

    Appreciate it thanks Mike

  17. #17
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Clear cell contents from a function

    I found the problem, the collection has to be declared as a Public variable.
    Here is the corrected code, including the ThisWorkbook code with proper declaration syntax.


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    If I were using code like this in a project, I'd do it differently. ClearIfOne would have a second Trigger boolean argument.

    But, as posted, its an example of how to control the clearing of a cell with a UDF.
    A similar technique can be used to color cells or other sheet altering changes that are normally forbidden to a UDF.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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