+ Reply to Thread
Results 1 to 9 of 9

Need to delete content in cell but keep all formulas

  1. #1
    Registered User
    Join Date
    11-09-2019
    Location
    Milan, Italy
    MS-Off Ver
    office 365
    Posts
    69

    Need to delete content in cell but keep all formulas

    good evening everybody,

    I am trying to figure out a way to delete data from cells but to keep the formulas.

    This is the macro I am using

    "Private Sub BtnNew_Click()

    Sheet1.Range("A2:T2,V2,X2,Y2,Z2,AA2,AB2,AC1,AD1,AE1,AC2,AD2,AE2,AF2").Select
    Range("AF2").Activate
    Selection.SpecialCells(xlCellTypeConstants, 3).Select
    Selection.ClearContents
    Sheet1.Range("A2") = ""
    Sheet1.Range("B2") = ""
    Sheet1.Range("C2") = ""
    Sheet1.Range("D2") = ""
    Sheet1.Range("E2") = ""
    Sheet1.Range("F2") = ""
    Sheet1.Range("G2") = ""
    Sheet1.Range("H2") = ""
    Sheet1.Range("J2") = ""
    Sheet1.Range("K2") = ""
    Sheet1.Range("L2") = ""
    Sheet1.Range("M2") = ""
    Sheet1.Range("N2") = ""
    Sheet1.Range("O2") = ""
    Sheet1.Range("P2") = ""
    Sheet1.Range("Q2") = ""
    Sheet1.Range("R2") = ""
    Sheet1.Range("S2") = ""
    Sheet1.Range("T2") = ""
    Sheet1.Range("V2") = ""

    end sub"

    The problem is that when the cells contain no data I get an error message.

    Any help will be highly appreciated

    Attached please find the test file I am working on

    Thank you all
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    11-02-2016
    Location
    NY
    MS-Off Ver
    2010
    Posts
    459

    Re: Need to delete content in cell but keep all formulas

    if you're just clearing data, how bout [a2:v2]="" in vba ? PS no animals were hurt in this testing!! ;+ )

  3. #3
    Registered User
    Join Date
    11-09-2019
    Location
    Milan, Italy
    MS-Off Ver
    office 365
    Posts
    69

    Re: Need to delete content in cell but keep all formulas

    HI queuesef, by using that I clear the formulas that are in the cells too...

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Need to delete content in cell but keep all formulas

    You cannot clear the cell without clearing the formulas as well but you could edit the formula to only display a result if other conditions are met:

    =IF(OR(ISBLANK(A2),ISBLANK(B2)), "", A2-B2)

    Calculates A2 - B2 if both A2 & B2 contain data otherwise returns a null string.

    (Simplistic example ignoring text input instead of numbers)

  5. #5
    Registered User
    Join Date
    11-09-2019
    Location
    Milan, Italy
    MS-Off Ver
    office 365
    Posts
    69

    Re: Need to delete content in cell but keep all formulas

    Hi cytop, you mean that this way I don't have to clear them... sounds safer than what I was looking for... let me test it....

  6. #6
    Registered User
    Join Date
    11-09-2019
    Location
    Milan, Italy
    MS-Off Ver
    office 365
    Posts
    69

    Re: Need to delete content in cell but keep all formulas

    Hi Cytop, It works. The only thing is that when I insert the formula in Z2 and AA2 (calculation of Actual Ground Time) AF2 returns a #VALUE. I have checked the settings and they are ok and I have formatted all the cells as time hh:mm

  7. #7
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Need to delete content in cell but keep all formulas

    Probably because of the =TEXT( function is some cells. (Phone at the moment so couldn't look too closely).

    There are any number of error checking functions you can use, it's more a case of finding out about them and applying the most relevant. For example, another formula you could use is

    =IFERROR(AE2-AD2, "Data error")

    Which will simply calculate the referenced cells or display any other value you specify if there's any error, but that's not really what you want here. It's just an example.

    You need to validate the results of each cell working back from AF2
    Last edited by cytop; 04-29-2022 at 05:11 PM.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hello, try this …


    Quote Originally Posted by s7yzrs View Post
    The problem is that when the cells contain no data I get an error message.
    So obviously just checking if the first cell is empty like in this VBA demonstration according to your attachment :

    PHP Code: 
    Sub Demo1()
        
    With FlightDetails.UsedRange.Rows(2)
            If 
    Not IsEmpty(.Cells(1)) Then .SpecialCells(2).ClearContents
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Registered User
    Join Date
    11-09-2019
    Location
    Milan, Italy
    MS-Off Ver
    office 365
    Posts
    69

    Re: Need to delete content in cell but keep all formulas

    Hello everybody, sorry for the late reply but I had to work.

    It works. Thank you so much all for your help

+ 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. [SOLVED] Match Cell Content and Delete Cell Content If a Match is Found
    By aviatecar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2013, 01:29 PM
  2. IF same CONTENT FOUND IN CELL A THEN DELETE CONTENT IN B,C or D
    By mecutemecute in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-11-2013, 01:16 AM
  3. [SOLVED] delete cell content when other cell's content is removed
    By Pretpik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2012, 11:06 PM
  4. Delete cell content
    By tonyabou in forum Excel General
    Replies: 1
    Last Post: 10-17-2011, 03:53 PM
  5. Delete Cell Content
    By jamer02 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2009, 08:47 AM
  6. what code can delete cell content
    By colwyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2008, 08:19 AM
  7. Delete Row Based On Cell Content
    By amsnss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2008, 02:01 PM
  8. [SOLVED] Delete cell content
    By Chuck Neal in forum Excel General
    Replies: 3
    Last Post: 06-14-2006, 12:10 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