问题
I'm having a issues with copying a value from a cell that is using a function to get its value from another cell. Insert value into A1, A3=A1 and when ever the value of A3 changes the changes need to transfer automatically to column H. The issues i having is that if i type the value into A1 then A3 gets updated but the values do not transfer over, if i type the value into A3 then the H column does update.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A3").Address Then
' Get the last row on our destination sheet (using Sheet2, col A here)...
Dim intLastRow As Long
intLastRow = Sheet1.Cells(Sheet2.Rows.Count, "H").End(xlUp).Row
' Add our value to the next row...
Sheet1.Cells(intLastRow + 1, "H") = Target.Value
End If
End Sub
Please see image
I have also tried the following code.
Private Sub Worksheet_Change(ByVal Target As Range)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Private Sub Worksheet_Change(ByVal Target As Range)
None of the above seems to trigger of the change event. Please I can not use Value A1 for the Value change it has come from A3 value change. I have even thought of running a copy and paste macro to Trigger A3. But would really like the value change code to work i can get the change value to work from A3. Thanks in Advance.
回答1:
Worksheet_Change does not fire when a cell recalculates its formula. It fires when a cell's Value is assigned.
If you are assigning a value to A1, you should be looking at Range("A1").Address in your handler.
The are several Calculate events, but they fire when calculation is complete for the entire sheet, not for individual cells.
回答2:
Try disabling events while processing your Change. Your code might be getting itself in a loop.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = Range("A3").Address Then
' Get the last row on our destination sheet (using Sheet2, col A here)...
Dim intLastRow As Long
intLastRow = Sheet1.Cells(Sheet2.Rows.Count, "H").End(xlUp).Row
' Add our value to the next row...
Sheet1.Cells(intLastRow + 1, "H") = Target.Value
End If
Application.EnableEvents = True
End Sub
回答3:
The following script will run every time the calculation on the sheet is finished, for more info see this link and it will then update Colunm H with the value in A3.
Not sure if it is exactly what will help but might get you on the right path. You can also build a check in to verify if the new A3 value is the same at the last Column H value and if they are then you could bypass the inserting of the value.
Private Sub Worksheet_calculate()
' Get the last row on our destination sheet (using Sheet2, col A here)...
Dim intLastRow As Long
intLastRow = Sheet1.Cells(Sheet2.Rows.Count, "H").End(xlUp).Row
' Add our value to the next row...
Sheet1.Cells(intLastRow + 1, "H") = Sheet1.Range("A3").Value
End Sub
来源:https://stackoverflow.com/questions/48028828/excel-vba-byval-target-as-range-does-not-update-target-worksheet-when-excuting-f