简体   繁体   English

从功能单元中提取Excel VBA ByVal目标范围时不会更新目标工作表

[英]Excel VBA ByVal Target As Range Does not update Target Worksheet when excuting from a function cell

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. 将值插入到A1中,A3 = A1,并且无论何时A3的值更改,更改都需要自动转移到H列。我遇到的问题是,如果我将值键入A1,则A3会被更新,但值不会转移,如果我将值输入A3,则H列会更新。

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. 请我不能使用值A1进行值更改,因为它来自A3值更改。 I have even thought of running a copy and paste macro to Trigger A3. 我什至考虑过将复制和粘贴宏运行到触发器A3。 But would really like the value change code to work i can get the change value to work from A3. 但是我真的希望值更改代码能够正常工作,我可以从A3获得更改值。 Thanks in Advance. 提前致谢。

Worksheet_Change does not fire when a cell recalculates its formula. 当单元格重新计算其公式时,不会触发Worksheet_Change It fires when a cell's Value is assigned. 分配单元格的Value时触发。

If you are assigning a value to A1 , you should be looking at Range("A1").Address in your handler. 如果要为A1分配值,则应在处理程序中查看Range("A1").Address

The are several Calculate events, but they fire when calculation is complete for the entire sheet, not for individual cells. 这些是几个Calculate事件,但是当整个工作表(而不是单个单元格)的计算完成时会触发。

Try disabling events while processing your Change . 在处理您的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

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 . 每当工作表上的计算完成时,将运行以下脚本,有关更多信息,请参见此链接 ,然后Colunm HA3的值更新Colunm H

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. 您还可以建立一个签入,以验证新的A3值在最后一个Column H值处是否相同,如果相同,则可以跳过该值的插入。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel-VBA - 如何识别在Worksheet_Change函数中删除目标范围(超过1个单元格)? - Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function? 在我特别点击目标范围之前,Worksheet_Change(将目标作为范围)不会更改单元格 - Worksheet_Change(Byval target as range) does not change the cells until I click the target range spesifically Worksheet_Change(ByVal目标为范围),目标始终为空 - Worksheet_Change(ByVal Target As Range), Target always equals Nothing Worksheet_SelectionChange(ByVal目标为范围),目标中有两个单元格 - Worksheet_SelectionChange(ByVal Target As Range), Two Cells in Target 难以在一个Excel工作表中合并2 x Private Sub Worksheet_Change(ByVal目标为范围) - Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet 将2个“ Private Sub Worksheet_Change(按目标的ByVal目标)”合并为1个 - Combining 2 “Private Sub Worksheet_Change(ByVal Target As Range)” into 1 从公共子调用(运行)私有子worksheet_Change(ByVal目标为范围) - Calling(run) a private Sub worksheet_Change(ByVal Target As Range) from public sub 在一个工作表中运行多个'Private Sub Worksheet_Change(ByVal Target As Range)' - Running multiple 'Private Sub Worksheet_Change(ByVal Target As Range)' in one worksheet 如何测试目标单元格是否在多个单元格的范围内:Excel VBA - How to test if a Target cell is in a range of multiple cells: Excel VBA 在VBA Excel上更改目标单元格 - Changing target cell on VBA Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM