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
Related
I'm currently working on a sheet that has to automatically insert todays date in a cell, if another cell is = "Yes"
I currently have this line of code (that I found online):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Not Intersect(Target, Range("G:G")) Is Nothing Then
Application.EnableEvents = False
For Each cell In Target
cell.Offset(0, 4).Value = Now
cell.Offset(0, 4).NumberFormat = "dd/mm/yyyy"
Next cell
End If
Application.EnableEvents = True
End Sub
The problem is that the updated cell in row K is being updated every time the cell is changed, and it should only be updated when the cell in row G = "Yes"
I appreciate the help :)
Your basic problem is solved easily - just add an If to check the content of the cell:
For Each cell In Target
If UCase(cell.Value2) = "YES" Then
cell.Offset(0, 4).Value = Now
cell.Offset(0, 4).NumberFormat = "dd/mm/yyyy"
Next cell
Next cell
However, your check for column 'G' is flawed. Target contains all cells that are currently modified. If the user enter something into a cell, Target will contain exactly that cell. If, however, data is for example pasted into that sheet, Target will contain all cells where data is pasted into.
Now, Intersect checks if two ranges have common cells. Your statement If Not Intersect(Target, Range("G:G")) Is Nothing will check if any of the modified cells is in column G and if yes, it will write the date into the cell that is 4 columns to the right. In the case the user enter something into a cell of column G, that's okay. But if he pastes something into, let's say, cells of columns F,G,H, the code will run for all three cells. So you should check each cell individually.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo change_exit ' Ensure that events are re-enabled in case of error
Application.EnableEvents = False
Dim cell As Range
For Each cell In Intersect(Target, Range("G:G"))
If UCase(cell.Value2) = "YES" Then
cell.Offset(0, 4).Value = Now
cell.Offset(0, 4).NumberFormat = "dd/mm/yyyy"
end if
Next cell
change_exit:
Application.EnableEvents = True
End Sub
Update: Changed the logic by just looping over the cells of target that intersect with column G - thanks to BigBen for the hint.
Consider:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Not Intersect(Target, Range("G:G")) Is Nothing Then
Application.EnableEvents = False
For Each cell In Target
If cell.Value = "Yes" Then
cell.Offset(0, 4).Value = Now
cell.Offset(0, 4).NumberFormat = "dd/mm/yyyy"
End If
Next cell
End If
Application.EnableEvents = True
End Sub
We test the value of each entry!
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 have a cell in Excel that gets updated on basis of RAND() function.
Each time I update the cell, I want to capture the previous values in a separate column. So that I can compute mean later on.
How to achieve this??
Copy below into Worksheet module and change "A1" cell:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range("A1")) Is Nothing) Then
i = i + 1
Worksheets("Sheet1").Cells(i, 2).Value = Worksheets("Sheet1").Cells(1, 1).Value
End If
End Sub
I need the active cell to have automatically the actual year as soon as a cell in the range A2:A20 is selected. My VBA code changes all the cells at once.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 2 To 20
If ActiveCell = Cells(i, "A") And Cells(i, "A") = "" Then
Cells(i, "A").Value = Year(Now)
End If
Next i
Range("A:A").EntireColumn.AutoFit
End Sub
Since you want that A2:A20 to have the current year, automatically when the cell is selected, you need to use the Worksheet_SelectionChange event. Then, in that event, you simply test if the cell that was selected is within the A2:A20 range and set the value to the current year if it is.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Me.Range("A2:A20"),Target) Is Nothing Then
Target.Value = Year(Now())
End If
Range("A:A").EntireColumn.AutoFit
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