Formula if value is deleted?

kcmuppet

Active Member
Joined
Nov 2, 2005
Messages
437
Office Version
  1. 365
Platform
  1. Windows
Hello everybody, I'd greatly appreciate some help

I’d ideally like to have some cells that can have a formula which can be overwritten with a manual entry, but that reverts back to the formula if the value is deleted/cleared.

E.g, in the following…

‘Product’, in column A, is a text, entered with a list validation
‘Unit cost’, in column B, is derived from A1 with a vlookup
‘Quantity’, in column C, is manually entered, and
‘Price’, in column D, is B*C

…I would really like to have it so I can either generate B and D using manual entries in A and C, or overtype them, but also have them revert back to the formula if A is deleted/cleared.

I’ve been searching around the forum here, and I wondering if there is some kind of worksheet event that could be used to trigger the reverting.

Should it be along these lines?:
Code:
Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$D$1" Then
    If Target.Value =0 Then
         Range("D1").Value = B1 * C1
    End If
End If
End Sub
(You can see I’m a bit clueless with coding, but hopefully there’s enough to get the gist of what I’m trying to do) :confused:

There’s a sort of similar discussion here:
http://www.mrexcel.com/board2/viewt...rder=asc&highlight=overwrite+formula&start=10
...but I don’t really understand it, since it sort of goes in a different direction.

Any help much appreciated
 
Joe Was said:
This code will work the 1st time you change D1 to Blank or Zero, but from then on the Event will not let you change the value...if you do want the Cell D1 to sometimes show a Blank or Zero you need to run the code from a Sub not an event!

Joe, I'm not sure what you mean by this. When I ran your code in an otherwise empty workbook, it all worked just fine. However, when I tried to add it to a sheet which already had some code on it, it didnt work. Is that what you mean? The sheet already has this code on it (which swaps a picture, depending on a lookup/cell validation):
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$B$45" Then Range("L12").Value = Target.Value
End Sub
Private Sub Worksheet_Calculate()
    Dim oPic As Picture
       Me.Pictures.Visible = False
       With Range("K1")
           For Each oPic In Me.Pictures
               If oPic.Name = .Text Then
                   oPic.Visible = True
                   oPic.Top = .Top
                   oPic.Left = .Left
                   Exit For
               End If
           Next oPic
        End With
 End Sub
I tried adding it at the bottom, and also just inserting it after the first If statement, which I guess didnt work because the the two range conditions would conflict. However, it didn't "not let you change the value", it just didnt return to the formula, once the value was deleted or returned to zero.

(bandit, I've got your method working, thankyou. But I want to see if it can be done without using comments).
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Yeah i can understand that. I didnt want to use comments but i never came up with or found a way to accomplish the task without it.
 
Upvote 0
Hello again Bandit,

I'm struggling with this:
bandit_1981 said:
If you only want that code to run on those cells you might try using target.addess.

IE:
if target.address = "B2" then
override code here
end if

Cant say if that will work but it should. I wrote teh code to allow all forumlas on my sheet to be overrode.
Should this be in the code on the sheet or the module?

I've tried both, but I can't be getting the placement exactly right since when I do it, I can only get it to either protect all the cells or none.
 
Upvote 0
bandit_1981 said:
I would put the code in the worksshet selection change and worksheet change event.
Bear with me on this... :confused:
Putting it straight after the 'Private sub' and closing it just before the 'End Sub' doesnt seem to work
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "D21:D39,L21:L39" Then
    If Target.HasFormula = True Then
        OldValue = Target.Formula
    Else
        OldValue = ""
    End If
    OldAmt = Target.Value
    OldAddr = Target.Address
 
On Error Resume Next
        If Selection.Cells.Count > 1 Then
        Exit Sub
        End If
     Call Override(ActiveSheet.Name, Target.Address, OldValue, OldAddr, OldAmt)
End If
End Sub
What am I doing wrong?
 
Upvote 0
I would say it is the way you are using target.adress it doesnt return a value in the way you are trying to compare it. If you walk through the code you will see the value you get as you tab through the cells. It will return that cells address. IE D25. It wont konw that address is in the range you are telling it. You could use it like this

target.address = "D21" or target.address = "D22" etc.
 
Upvote 0
bandit_1981 said:
You could use it like this

target.address = "D21" or target.address = "D22" etc.

I'm not having much luck with that. I think maybe I'll need to use an intersect. Maybe something like this:
Code:
If Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
    Exit Sub
End If
...I'll experiment & report back.

In the meantime, does your code disable undo in your whole workbook, by any chance?
 
Upvote 0
I dont konw anything about intersect but good luck and no it doesnt disable my undo. Do you have your sheets protected?
 
Upvote 0
bandit_1981 said:
Do you have your sheets protected?

They're not protected, though when I've finished building them, my intention is to protect some of them. Can the code cope with that?

Can the code in the module part go onto just the one sheet I want it to apply to? i.e be on the sheet instead of in a module?

Or can I change this...

ThisWorkbook.Worksheets(strSheet)

...to reflect just one named sheet?
 
Upvote 0

Forum statistics

Threads
1,215,565
Messages
6,125,583
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top