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
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
I'm trying to create a cell in Excel that resets every time I put a value into it and every value that I put in the cell is stored and added together.
So basically cell A1 would be empty and then I add a value 30, for example. The cell would then store that value and reset to receive more inputs. I then go ahead and put another value in cell A1, 20. The cell should once again reset, but the value stored in cell A1 would now be equal to 50.
I'm very new to VBA so I'm still trying to figure everything out. I tried using some code I found in another post, but was not able to make it work so I was wondering if anyone had any idea on how to proceed with this problem.
This is the code I found and wasn't able to make it work. It was supposed to receive a value in cell A1 and store the same in cell A2, and once you add a new value to A1, it adds it to the previous value in A2.
Private Sub Worksheet_Change(ByVal Target As Range)
If Cells(1, 1).Value <> gdDouble Then
gdDouble = Cells(1, 1).Value
Cells(2, 1).Value = Cells(2, 1).Value + Cells(1, 1).Value
End If
End Sub
Private Sub Workbook_Open()
gdDouble = Sheets("sheet1").Cells(1, 1).Value
End Sub
And in the standard module:
dim gdDouble as double
Thank you
Adjust the code in the worksheet_change event like that
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
'If Target.CountLarge > 1 Then Exit Sub
On Error GoTo EH
Application.EnableEvents = False
Target.Value = Target.Value + gdDouble
gdDouble = Target.Value
EH:
Application.EnableEvents = True
End Sub
And change gdDouble to a public variable
Public gdDouble As Double
I am trying to make cell E23 as a user input and as a formula cell if user does not enter any value. For eg: If user enters a value in cell E23, then consider that value. If user does not enter any value then copy value from cell B23. Below is the vba code which I tried.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$23" And Target.Value = "" Then
Target.Formula = "=B23"
End If
The code works fine until I change the value in cell B23 by a selection made in the dropdown combobox. When I change the selection in combobox from option 1 to option 2, new value gets updated in cell B23 which must be copied into E23. But it gives me a runtime error '13' Type mismatch.
Any help is appreciated. Thank you
The issue here is that this line
Target.Formula = "=B23"
changes the target cell, and that triggers a Worksheet_Change event that changes the target cell, and that triggers a Worksheet_Change event … and so on.
So you need to disable the events before you change the target cell (and re-enable them afterwards).
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$23" And Target.Value = vbNullString Then
Application.EnableEvents = False
Target.Formula = "=B23"
Application.EnableEvents = True
End If
End Sub
Alternative that works also when multiple cells are selected:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E23")) Is Nothing Then
Application.EnableEvents = False
If Range("E23").Value = vbNullString Then
Range("E23").Formula = "=B23"
Else
Range("E23").Value = Range("E23").Value - Range("E17").Value
End If
Application.EnableEvents = True
End If
End Sub
I'm very new to VBA. I have a cell in the worksheet that live updates a value. the Value changes about once every month. Is there a way for me to record the day that the value changed in an adjacent cell?
For example,
If the value in A1 changes from 5 to 6 today, I just want to record today's date in A2.
I don't really need to keep a record of previous changes.
Thank you so much!
If you are using a Bloomberg function in cell A1 like BDP() or BDH() or BDS(), then you could use the Worksheet_Calculate() event macro to detect changes in that cell.
In this example, I use cell A3 as a "memory" to avoid re-posting the date too often:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If [A1] <> [A3] Then
[A3] = Range("A1").Value
[A2] = Date
MsgBox "Date recorded"
End If
Application.EnableEvents = True
End Sub
The Worksheet_Change() is being fired when something on the sheet changes its value, so add something like this to your Sheet-Codemodule:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Debug.Print "A1 has been changed!"
'do something
End If
End Sub
Update:
It seems you need the calculate event also as you're using a formula. you could try something like this:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
ActiveSheet.Calculate
DoEvents
With Range("A1")
.Value = .Value
DoEvents
End With
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Range("A2").Value = Date
End If
End Sub
You could use the Worksheet_SelectionChange event. I guess it depends on what else you're doing in the worksheet as to whether it would fire or not.
You'd have to compare the value in A1 against what was in A1 previously which would be stored in another cell.
My problem is the following:
The function below triggers an "if then function" when i manually change the value in cell D9. What should I do to get it to work with an automatic value change of cell D9 trough a link.
In other words if i where to link cell D9 to cell A1 and change the value of A1 can i still make the function below work?
Private Sub Worksheet_Change(ByVal Target As range)
If Target.Address = "$D$9" Then
If range("C12") = 0 Then
Rows("12:12").Select
Selection.RowHeight = 0
Else:
Rows("12:12").Select
Selection.RowHeight = 15
End If
End Sub
How about something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim rngDependents As Range
Set rngDependents = Target.Dependents
If Target.Address = "$D$9" Then
MsgBox "D9 has changed"
ElseIf Not Intersect(rngDependents, Range("$D$9")) Is Nothing Then
MsgBox "D9 has been changed indirectly"
End If
End Sub
try to make your function then in other cell use the function with input the link to the cell d9.
When you change the value at cell d9 your function will be evaluated.