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
Related
I have a cell which i declared as active cell using developers tool in excel
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("C1").Value = ActiveCell.Value
End Sub
this cell is equals to the value of a drop down cell i created from data validation.
My Problem is cell C1 is not changing when i select a new value from the drop down
Here you go. When B1 is changed C1 is assigned the value of B1. It doesn't matter how the change was carried out, from the keyboard or by selection from a drop-down.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("B1").Address Then
' prevent the coming change from causing a call of this procedure
Application.EnableEvents = False
Cells(1, "C").Value = Target.Value
Application.EnableEvents = True
End If
End Sub
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
I have two drop down lists -- one is dependent on the other -- meaning if I select a particular value from list in A1, a specific list appears in A2. This works just fine. However, when I change the value in A1, A2 stays in the cell until I click on the list -- then my value in A2 will change based on my selection.
For example, if list 1 is ['Yes','No'] and list to is Yes: [1,2,3] No: [4,5,6]. First I select 'Yes' for A1 and then select 2 for A2. Then, if I select 'No' for A1, "2" stays in A2 until I actually click on A2 to select a new value (4,5,6). Is there any way to "clear" A2 once I've changed the A1 selection?
Thanks!
put this in the VBA code for your worksheet:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = Range("A1").Address Then
Dim dependentCell As Range
Set dependentCell = Target.Offset(1, 0) 'Cell A2
If dependentCell.Validation.Value = False Then dependentCell.Clear
End If
End Sub
Put this code in the code page for your Worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
Range("A2").ClearContents
End If
End Sub
You could insert code for refreshing A2 list in worksheet_change procedure for your worksheet. Every time a cell value is changed in that worksheet, your updating code will run.
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
In cell a1 value is always changing. I want to keep track of minimum value of a1 in a2. How is it possible?
Store the value of A1 in A2.
Implement an event which responds to the change in A1.
When the event is fired, compare A1 to A2, and if A1 is smaller, store the new value.
This assumes there's already a value in A2:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Dim a2val As Variant
Dim a1val As Variant
a2val = Range("$A$2").Value2
a1val = Range("$A$1").Value2
If a1val < a2val Then
Range("$A$2").Value2 = a1val
End If
End If
End Sub