+ Reply to Thread
Results 1 to 8 of 8

Code to delete cell contents IF the result of a formula in the cell = 0

  1. #1
    Registered User
    Join Date
    01-20-2013
    Location
    New Zealand
    MS-Off Ver
    2016
    Posts
    19

    Code to delete cell contents IF the result of a formula in the cell = 0

    I am very new to VBA so please be gentle with me

    I have a worksheet that already contains a “Private Sub Worksheet_Change(ByVal Target As Range)” procedure (set up by someone else).

    I now want to add into the worksheet some code that automatically deletes cell contents IF the result of a formula in the cell = 0. The formula in the cell is =IF(A2<>"",A2-C2," ") and the formula is continued down column D. I don’t know whether this can be added onto the existing procedure or if it should be a new one.

    The reason I want the cell contents to be deleted is if the formula result is 0 then I don’t want the 0 to count in a Pivot Table I run from the database.

    Some additional info (in case it makes a difference) is that a figure will be entered into column A initially and then at a later date another figure could be entered into column C that produces the 0 result in D. At this point I want the cell contents cleared.

    I hope this makes sense. I have attached a very simple example.

    Example.xlsx

    Thanks in advance, Janet

  2. #2
    Registered User
    Join Date
    01-21-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    try this..

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-20-2013
    Location
    New Zealand
    MS-Off Ver
    2016
    Posts
    19

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    Thank you - I appreciate the reply. That certainly works & clears all the cells in column D with a 0 result if I run the code. What I was really hoping for though is code that will automatically clear the cell contents as soon as they result in 0 (i.e. as it happens and without having to maually run the code). Any further ideas on that basis?
    Thanks again.

  4. #4
    Registered User
    Join Date
    01-21-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    =if(a2<>"",a2-c2,"0")

  5. #5
    Registered User
    Join Date
    01-20-2013
    Location
    New Zealand
    MS-Off Ver
    2016
    Posts
    19

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    Thank you. I assume what you are saying here is to enter this as a formula D, but that just "hides" the 0 result. It still counts as a number as the cell is not really blank - it just looks like it is.

  6. #6
    Registered User
    Join Date
    01-21-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    There is no way to remove a formula unless you use a macro to run it. you can set auto run macro to help u achieve your result once you open the worksheet.

    1. Go to the Visual Basic Editor by pressing the keys Alt+F11.

    2. From the Project Explorer, double-click over the ThisWorkbook object

    3. That will execute an event window for you. At the top of the new code window, you will see the word 'General'. Select the drop down where you see general and select 'Workbook'.

    4. You will notice that Excel VBA assumes the Open event. That means whatever code you insert in this procedure will execute automatically when the workbook is opened.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-20-2013
    Location
    New Zealand
    MS-Off Ver
    2016
    Posts
    19

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    That certainly works and deletes any 0 formula results when the workbook opens! Thank you very much for your patience - I really appreciate it

  8. #8
    Registered User
    Join Date
    01-21-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Code to delete cell contents IF the result of a formula in the cell = 0

    you are welcome. (:

+ 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