Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping formulas in excel fields 2

Status
Not open for further replies.

Crashtestdummy

Materials
May 8, 2002
10
Hey Excel gods,

is there any way of entering a formula in an excel field, so that if a single entry is made it will override the formula, but if the field is then cleared that the original formula returns. For example : if A1 hold the formula "@sum(F3:F9)" but the user will be able to override this with a value, say the user enters "5" in A1. The next user might not like this and clear the "5". Is it now possible for the field A1 to go back to its original formula of "@sum(F3:F9)"?

Thanks in advance.

Crash
 
Replies continue below

Recommended for you

What you are looking for is impossible, unless you use a function that opens a dialog box where the user can choose to input a value or to use the standard provided formula. prex
motori@xcalcsREMOVE.com
Online tools for structural design
 
This can be done with VBA, if you are interested. Just use the Worksheet_Change event to monitor the target cell. Simply insert this code in the sheet's object module.
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.AddressLocal = "$A$1" Then
        If Len(Target.Text) > 0 Then
            Exit Sub
        Else
            Target.Formula = "=SUM(F3:F9)"
        End If
    End If
End Sub
You can also add some additional checking to validate the contents of the cell (i.e. only allow numerical entries).

Hope this helps... DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Or you could do it the ugly way in Excel

user enters a value or puts 0 in cell A1

then A2 is if(A1=0,some formula,A1)

nasty but quick





Cheers

Greg Locock
 
why wouldnt the user just use "undo"?

Best Regards

Morten
 
Thank you dsi!!

your code has helped alot. many thanks. Just one more question. it puts the formula result in the the field after the the field is changed to 0. Is there a way of it doing that check when the sheet is made active? I tried using the worksheet_activate event but I get compiling errors if I use that.

Thanks again
 
If you want to accept a value of zero, you can not use the contents check in the earlier example. That simply checks to make sure the cell is not empty. Here is an alternative:
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.AddressLocal = "$A$1" Then
        If IsNumeric(Target.Text) = True Then
            If Target.Value = 0 Then Exit Sub
        ElseIf Len(Target.Text) > 0 Then
            Exit Sub
        Else
            Target.Formula = "=SUM(F3:F9)"
        End If
    End If
End Sub
As for processing the check upon activating the sheet, use the Worksheet.Activate event to call the Change event:
Code:
Private Sub Worksheet_Activate()
    Call Worksheet_Change(Range("A1"))
End Sub
Good luck... DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor