简体   繁体   English

使用上次编辑的单元格的位置更新单元格-Excel VB错误28“堆栈空间不足”

[英]Update Cell With Location of Last Edited Cell - Excel VB Error 28 “Out of stack space”

I am trying to essentially get one specific cell to update with the address of the most recently edited cell. 我实际上是想让一个特定的单元格更新为最近编辑的单元格的地址。 I am running into the out of stack space error and I am not sure why. 我遇到堆栈空间不足错误,我不确定为什么。 I'm guessing maybe a call isnt finishing and so it just fills the stack with each call? 我想也许一个电话还没有结束,所以每次电话都充满了电话吗?

I have the following code: 我有以下代码:

Worksheet Code 工作表代码

Private Sub Worksheet_Change(ByVal Target As Range)

    Call Module1.last_changed(Target)

End Sub

Module Code 模块代码

Public LASTCHANGED As Range

Public Function last_changed(changedCell As Range)

    If LASTCHANGED Is Nothing Then
        Set LASTCHANGED = changedCell
    Else
        LASTCHANGED = changedCell
    End If

    Call last_changed_address

End Function


Public Function last_changed_address()

    Dim address As Variant
    address = Split(LASTCHANGED.address, "$")
    Sheet1.Range("A23").Value = address(1) + address(2)

End Function

I also have a global variable which will hold the location of the last edited cell. 我还有一个全局变量,它将保存最后编辑的单元格的位置。 If there is a better way of doing this than a global variable I am open to discussion. 如果有比全局变量更好的方法,我可以进行讨论。 Thanks! 谢谢!

You are getting the out of stack space error because you are re-triggering the Worksheet_Change event when you change the value of Range("A23") cuasing an indefinite loop. 之所以出现堆栈空间不足错误,是因为当您更改Range(“ A23”)的值导致无限循环时,您正在重新触发Worksheet_Change事件。 You need to toggle EnableEvents to break the chain. 您需要切换EnableEvents才能打破链条。

This should replace all your code. 这应该替换所有代码。

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False

    Range("A23").Value = Target.address(False, False)

    Application.EnableEvents = True
End Sub 

Thank you Thomas, your code does exactly what I wanted the outcome to be. 谢谢托马斯,您的代码正是我想要的结果。 I just need to look into Scott's recommendation about storing variables in a reference sheet which I can access later. 我只需要研究一下Scott关于将变量存储在参考表中的建议,以后就可以访问。

I guess a parting question is, is there a way to do this without someone actually seeing the reference sheet show up? 我想一个离别的问题是,有没有办法在没有人真正看到参考表出现的情况下做到这一点?

Thanks for everyone's input I appreciate it! 感谢大家的投入,我非常感激!

To answer the original question, I used Thomas's code exactly. 为了回答最初的问题,我完全使用了Thomas的代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM