+ Reply to Thread
Results 1 to 3 of 3

Modifying a macro to only clear a cell if it contains data and not a formula

  1. #1
    Forum Contributor
    Join Date
    12-09-2011
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    201

    Modifying a macro to only clear a cell if it contains data and not a formula

    I have a spreadsheet which contains a cell (B4) where a currency amount can be entered (e.g. £325.00), and the cell is formatted as currency, with two decimal places, with a currency symbol. I've added a formula to that cell so that the currency figure can be automatically populated if the user uses a different tab in the same spreadsheet, or the user can just enter a figure directly and overwrite the formula, and that all works fine.

    The problem I'm having is, there is a button which runs a macro which clears the data from various cells, including cell B4:

    Union(Range( _
    "E27,E29,E31,F12:F18,F20,F23,F25,F27,F29,F31,B4,C4,D4,E4,F4,G4,I4:K16,B6:B8,C7,C7,C8,E6:E8,F7,F8,B11:B18,B20,B23,B25,B27,B29,B31,C12:C18" _
    ), Range("C20,C23,C25,C27,C29,C31,E11:E18,E20,E23,E25")).Select
    Range("F31").Activate
    Selection.ClearContents
    Range("A1:K1").Select

    What I would like is to modify these commands so that cell B4 is not cleared, but is instead returned to it's original state (where it contains the original formula), if it currently has a number amount in it (be it a number entered manually, or a number generated by the formula).

    Is there a way I can do this?
    Last edited by ianpwilliams; 05-16-2013 at 12:31 PM.

  2. #2
    Forum Contributor
    Join Date
    12-09-2011
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    201

    Re: Modifying a macro to only clear a cell if it contains data and not a formula

    I've created a separate macro (using the "Record Macro" option) which looks at the specified cell, and if there is a number there, then it will revert the cell back to it's formula state:

    Sub RESETFORMULAS()
    '
    If (IsNumeric("B4")) Then
    Range("B4").Select
    ActiveCell.FormulaR1C1 = _
    "=[FORMULA]"
    Range("B5").Select
    End If
    End Sub

    The macro runs fine, but it doesn't revert the cell back to it's formula state if it sees a number there.

  3. #3
    Forum Contributor
    Join Date
    12-09-2011
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    201

    Re: Modifying a macro to only clear a cell if it contains data and not a formula

    I'm talking nonsense, sorry. All I need is for the macro to automatically revert specific cells to their formulas, and I can do that using some of the code above. Sorry for the pointless thread!

+ 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