+ Reply to Thread
Results 1 to 5 of 5

. Clear Contents takes out the formula for the cell i need.

  1. #1
    Registered User
    Join Date
    08-20-2019
    Location
    nj, usa
    MS-Off Ver
    10
    Posts
    15

    . Clear Contents takes out the formula for the cell i need.

    I'm using the following code in the cells [CODE] =IF(ISBLANK(Z36),"",DATEDIF(Z36,TODAY(),"d")) [CODE] When I click a blank cell it clears out the formula i have in the adjacent cells. I want the formula to stay i just want the text or number to clearout.

    I assume because im using the .ClearContents its clearing everything in that cell even the formula. Any ideas?
    This is the vba code im using as well

    [CODE]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Select Case Target.Column
    Case 10
    Range("H" & Target.Row).Resize(, 2).ClearContents
    Case 16
    Range("N" & Target.Row).Resize(, 2).ClearContents
    Case 4
    Range("B" & Target.Row).Resize(, 2).ClearContents
    Case 22
    Range("T" & Target.Row).Resize(, 2).ClearContents
    Case 28
    Range("Z" & Target.Row).Resize(, 2).ClearContents
    Case 34
    Range("AF" & Target.Row).Resize(, 2).ClearContents

    End Select

    End Sub[CODE]


    I hope that makes sense
    Last edited by GRIM99; 08-23-2019 at 08:36 AM.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: . Clear Contents takes out the formula for the cell i need.

    ClearContents will remove whatever is in the designated cell, including formulas. If you want to remove the values created by the formulas, then you need to clear he contents of the precedent cell (The cell holding the value from which the formula derives the value of the target cell.) For example if you have in cell C3
    Please Login or Register  to view this content.
    And you want to remove the value from C3, then
    Please Login or Register  to view this content.
    Will change the value in C3 to empty string ("").

    this all assumes that you no longer need the values in Column A and B. When using formulas to create values based on values in other cells, it is the precedent cells which determine what shows in the dependent cells. The formul in the dependent cell can be written to show blank except for specific conditions by using the IF function.
    Last edited by JLGWhiz; 08-21-2019 at 10:11 AM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    08-20-2019
    Location
    nj, usa
    MS-Off Ver
    10
    Posts
    15

    Re: . Clear Contents takes out the formula for the cell i need.

    [CODE]Range("H" & Target.Row).Resize(, 2).ClearContents [CODE] what should come after that to keep the formula in the cell and just wipe the data for that cell.
    Last edited by GRIM99; 08-23-2019 at 08:37 AM.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: . Clear Contents takes out the formula for the cell i need.

    Let me try again to explain how it works.
    1. You cannot use clear contents on a cell with a formula and keep the formula.
    2. You can use a formula with an IF function (eg. If(A1>0, B2, "")), to set a condition that allows the cell to appear blank if the condition is not met.
    3. If you want a different value to appear in the cell, then the criteria cell (ie. A1), or the precedent cell (ie. B2) values would have to be eliminated so the formula would produce blank.
    4. You have to decide how you want to make the cell appear blank and keep the formula or
    5. Rethink your objective and do it a different way.

    BTW, I do not respond to private messages by private message. All of my comments are provided in a post so all can see them.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: . Clear Contents takes out the formula for the cell i need.

    Administrative Note:

    Not for the first time ...

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ 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. Clear merged cell contents in comand button(clear all)
    By mohan_984 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-25-2015, 10:39 AM
  2. [SOLVED] Macro to clear contents of a cell with a formula that is not returning a value
    By pasqualebaldi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2014, 03:34 PM
  3. [SOLVED] If cell contains formula then not clear contents problem
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2014, 02:36 AM
  4. Macro Formula for Checkbox to Clear other Cell Contents
    By MMAYBAUM in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-20-2014, 12:44 PM
  5. VBA code to clear the contents (or value) of a cell without clearing the formula
    By BazzaBit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-10-2013, 08:16 PM
  6. How to clear formula without clearing cell contents
    By prayami in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-03-2008, 04:33 AM
  7. clear cell contents if not a formula
    By pimar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2006, 10:03 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