How to capture a cell value in Excel when a cell updated - excel

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

Related

Accumulating values that are input into single Excel cell

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

Change Cell Value

I have a Dynamic Table that changes based on the value of B1.
I want the value of B1 to change by clicking on another cell from another workbook. I'm super new to VBA and don't really know what I'm doing.
What I want is to click on any cell from column E and it will change the value of B1 to equal value of column O.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E2:E")) Is Nothing Then
Worksheets("Dynamic Table").Range("B1").Value = ActiveCell.Offest(0, 10)
End If
End If
End Sub
My Amateur Code
Do you mean sth like that
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Cells.CountLarge = 1 Then
Worksheets("Dynamic Table").Range("B1").Value = Target.Offset(, 10).Value
End If
End If
End Sub
I assumed you mean with another workbook just another worksheet.

Pull data from a corresponding cell in row A in last edited row and insert into cell A1

I am looking for a script that will pull data from last edited cell into Cell B1 of active sheet and also look up data from cell in column A and then display it in cell A1.
So far I've got this to pull last edited cell into B1 and it works fine but I cannot figure out how to then go back from that point to row A and display the other info.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F13:W9910")) Is Nothing Then
ActiveSheet.Range("B1").Value = Target.Value
End If
End Sub
In the attached picture if I add any numbers in section called trays completed (in red) to display in B1 and then look up number in Sap column and display the number in cell A1
Something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("F13:W9910")) Is Nothing Then
Me.Range("B1").Value = Target.Value
Me.Range("A1").Value = Target.Entirerow.Cells(1).Value
End If
End Sub
Note when in a sheet code module you can refer to the worksheet using Me
Also be aware that Target might be >1 cell and your code might need to handle that.
This will take the corresponding value in A and place it in A1. I only added one line of code to yours.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F13:W9910")) Is Nothing
Then
ActiveSheet.Range("B1").Value = Target.Value
ActiveSheet.Range("A1").Value = ActiveSheet.Range("A" & Target.Row)
End If
End Sub

show numbers starting from 1 based on a cell value using vba

show numbers starting from 1 based on a cell value using vba.
For example, if you type 4 in cell A1, then range D1,D2,D3,D4 should display 1,2,3,4 respectively. If type 5 in cell A2, then range D1,D2,D3,D4,D5 should display 1,2,3,4,5.
Could anyone please help?
The below macro uses a worksheet change event to detect changes to the cell A1,
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And IsNumeric(Range("A1")) Then
Dim i As Long
Columns(4).ClearContents
For i = 1 To Cells(1, 1)
Cells(i, 4) = i
Next i
End If
End Sub

Update cell when selected

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

Resources