Excel formula to set cell value - excel

I've got two columns in my Excel spreadsheet that I want to be mutually exclusive.
e.g.
B1 formula: IF(A1 contains "yes" then B1 set to "-") and also
A1 formula: IF(B1 contains "yes" then A1 set to "-")
Basically this should mean that when one cell is changed to "Yes", the other cell beside it automatically changes to"-"
When I try to do this in the excel formula builder it says that I've created a circular formula. Does anyone know what I can do to make this work?
Thanks

How about something like this:
' On cell A1
=IF(B1="Yes","-","Yes")
' On cell B1
=IF(A1="Yes","-","Yes")
The downside is that after A1 is given a value of "Yes" or "-", it no longer has the function as its value. This potentially isn't a issue - just leave B1 with the function and the set A1 as needed (B1 will update so you don't need to update it)
Not exactly your request, but hopefully it will help someone someday!

This can only be achieved in VBA. Copy the following code in the module of your worksheet (e.g. Sheet1):
Private mBlnIsRunning As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.Intersect(Target, Me.Range("A1:B1")) _
Is Nothing Then Exit Sub
'Prevent endless loop
If mBlnIsRunning Then Exit Sub
mBlnIsRunning = True
If Target.Address(False, False) = "A1" Then
Me.Range("B1") = Negative(Target.Value)
Else
Me.Range("A1") = Negative(Target.Value)
End If
mBlnIsRunning = False
End Sub
Private Function Negative(varValue As Variant) As Variant
'Change this function according to your needs
If varValue = "yes" Then
Negative = "-"
ElseIf varValue = "-" Then
Negative = "yes"
Else
Negative = "Please enter either yes or -!"
End If
End Function

Related

Update cell protection based on another cell's value

I am trying to track data in a laboratory.
Goal. When the value in cell P3 changes to "yes", then cells Q3:AE3 are locked. However, if "yes" does not appear in cell P3, then cells Q3:AE3 are unlocked.
I need to loop through column P locking/unlocking cells with respect to the row each P value is located.
For example if P36 = "Yes", the Q36:AE36 would become locked.
Edit: This code works with line P3. How can I make this loop through P3:P500?
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("P3") = "Yes" Then
Range("Q3:AE3").Locked = True
ElseIf Range("P3") = "No" Then
Range("Q3:AE3").Locked = False
ElseIf Range("P3") = "" Then
Range("Q3:AE3").Locked = False
End If
End Sub
This will handle any number of values being changed in column P. Provide your password as needed.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'did any cells in column P change
If Not Intersect(Target, Me.Range("P1").EntireColumn) Is Nothing Then
Me.Unprotect "password"
Dim cell As Range
For Each cell In Intersect(Target, Me.Range("P1").EntireColumn)
Me.Range("Q" & cell.Row & ":AE" & cell.Row).Locked = UCase$(cell.Value) = "YES"
Next
Me.Protect "password"
End If
End Sub

select cell in range inputs value into cell in another sheet

In the code below that i got working, when i select a cell in a range, an input box pops up and my input is sent to the same cell i clicked but in sheet 2. I want to take this one step further. i want to bypass the input box completely and just send the value F, and i only want to do this after i click cell b2. so cell b2 would have to work as some kind of toggle (maybe put an invisible shape over it to act as a button?)
Example 1: sheet 1, select cell B2 turns on macro, select cell in range example: D10 inputs the letter F into cell D10 on sheet 2, select cell B2 turns off macro so if i select cell D10 or any cell in that range nothing will happen anymore. it would also need to remove the value F from D10 if the cell is clicked again while the macro is on.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRtn = Application.InputBox("Insert your value please")
Sheets("2020").Range(Target.Address).Value = xRtn
End If
End If
End Sub
Untested. I'm not sure if I understood all of your objectives.
I think if you add a checkbox to your worksheet (resize and store it wherever you want; maybe in cell B2) called "Check Box 1" then the below code should work.
One way of adding a check box might be: Excel > Developer > Insert > Check Box (Form Control) (depending on your Excel version). If the Developer tab is not visible, you may need to get it to show first.
Option Explicit
Private Function GetCheckBox() As Shape
Set GetCheckBox = Me.Shapes("Check Box 1")
End Function
Private Function IsCheckBoxTicked() As Boolean
IsCheckBoxTicked = (GetCheckBox.OLEFormat.Object.Value = 1)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge <> 1 Then Exit Sub
If Intersect(Target, Me.Range("D9:AS20")) Is Nothing Then Exit Sub
If IsCheckBoxTicked() Then
With ThisWorkbook.Worksheets("2020").Range(Target.Address)
.Value = IIf(.Value = "F", Empty, "F")
End With
End If
End Sub

Update value in Excel Field based on Value in another

I am trying to update the value of Cell A1 to reflect the value in B1, but without using formulas in Cell A1. For Example, if the value in B1=5, then By default the value in A1 should be updated to 5. The user needs to be able to type in a number in A1 if B1 is blank, but once B1 has a value, then A1 should always reflect that value. I have tried VBA but it doesn't work. I am hoping there is a way to do this without VBA. Any help is appreciated.
It isn't possible without using VBA
To copy B1 to A1 if B1 isn't empty using VBA (every time a cell is changed)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsEmpty(Range("B1").Value) Then
Range("A1").Value = Range("B1").Value
End If
End Sub
Same thing but if B1 becomes empty, it clears A1
Private clear As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsEmpty(Range("B1").Value) Then
clear = True
Range("A1").Value = Range("B1").Value
ElseIf clear = true Then
clear = False
Range("A1").Value = ""
End If
End Sub

How to sum two cells into one and delete the formula after that?

I have this very niche problem. For instance Lets say the value in the cell A1 is 5 and A2 is 10, what I want to do is add A1 to A2 i.e A2 is now 15 and then when I delete the value in A1 the sum should remain in A2. I cannot use the paste special option to copy just the values because i want to retain the formula for later use i.e when I again input a value to A1 lets say 3 then the sum in A2 should be 18. Any help???
VBA code that triggers this sort of action will be helpful
Thanks In advance.
You just set the Value:
Range("A3").Value = Range("A1").Value + Range("A2").Value
In the worksheet's code sheet (right-click worksheet name tab then View, Code).
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1, A2")) Is Nothing Then
On Error GoTo bm_Safe_Exit
Application.EnableEvents = False
if application.count(Range("A1, A2")) = 2 then
Range("A3") = application.sum(Range("A1, A2"))
end if
End If
bm_Safe_Exit:
If CBool(Val(Err.Number)) Then _
Debug.Print Err.Number & ": " & Err.Description
Application.EnableEvents = True
End Sub

Add value from one cell to another then reset cell value. Excel

Hope someone can answer us.
We want to do the following:
When a value is entered in A1, this value is added to the value of B1.
We then want the value of A1 too reset to 0, but keep the value of B1
Is this possible in excel?
On the worksheet's VBA private module (right-click the report tab and hit "View Code"), enter the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = Range("a1").Address Then
Range("b1") = Range("b1") + Range("a1")
Range("a1").ClearContents
End If
Application.EnableEvents = True
End Sub
Worksheet_Change is called whenever a change is made to the worksheet.
Application.EnableEvents=False prevents the code from running continuously (without it, the change to B1 would also call Worksheet_change).
This code assumes that the value in B1 is always numeric (or blank). If it may contain character text, then a check will need to be put in place to either reset its value or not do the increment.
Using simoco's suggestion:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range
Set A1 = Range("A1")
If Intersect(Target, A1) Is Nothing Then
Else
Application.EnableEvents = False
With A1
.Offset(0, 1) = .Offset(0, 1) + .Value
.ClearContents
End With
Application.EnableEvents = True
End If
End Sub

Resources