简体   繁体   English

Excel VBA:由于某种原因变量重置为0

[英]Excel VBA: Variables resetting to 0 for some reason

here's my current code: 这是我当前的代码:

Private Sub Workbook_Open()
sumn1 = Sheets("Main").Cells(1, 1).Value
sumn2 = Sheets("Main").Cells(2, 1).Value
sumn3 = Sheets("Main").Cells(3, 1).Value
sumn4 = Sheets("Main").Cells(1, 2).Value
sumn5 = Sheets("Main").Cells(2, 2).Value
sumn6 = Sheets("Main").Cells(3, 2).Value
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Sheets("Main").Cells(1, 1) = sumn1
    Sheets("Main").Cells(2, 1) = sumn2
    Sheets("Main").Cells(3, 1) = sumn3
    Sheets("Main").Cells(1, 2) = sumn4
    Sheets("Main").Cells(2, 2) = sumn5
    Sheets("Main").Cells(3, 2) = sumn6
End Sub

So, while the workbook is open and it's being worked in, after some actions the variables sumn1, sumn2..etc. 因此,当打开工作簿并在工作簿中工作时,在执行一些操作后,变量sumn1,sumn2..etc会变。 (which are global variables) are getting added values, like +10 to sumn1, or +5 to sumn2 and so on. (它们是全局变量)正在获得附加值,例如+10到sumn1或+5到sumn2,依此类推。 Since I want them saved after closing the workbook, I save them in a cell that I have hidden with ";;;". 由于我希望在关闭工作簿后将它们保存,因此我将它们保存在用“ ;;;”隐藏的单元格中。

The problem is, sometimes it works correctly, but sometimes (usually after longer time since workbook has been closed since) the variables reset to 0. 问题是,有时它可以正常工作,但有时(通常是自从关闭工作簿以来等待了较长时间),变量重置为0。

So first of, is my approach an alright one on how I am saving the data or its just not working because this approach is bad? 因此,首先,对于我如何保存数据还是因为这种方法不好而导致我的方法不起作用,我的方法是否正确? If it's an alright approach then I guess I should search for my mistake somewhere else then. 如果这是一个好的方法,那么我想我应该在其他地方搜索我的错误。

All your variables are also reset to zero when "reset" is clicked within the VBA editor, or an "End" statement is reached. 在VBA编辑器中单击“重置”或到达“结束”语句时,所有变量也都重置为零。 There could be other occasions too although I can't think of any. 尽管我想不到,也可能还有其他场合。 You are currently only guarding against the workbook being closed and reopened. 您当前仅在防止工作簿被关闭并重新打开。

So your approach is brittle. 因此,您的方法很脆弱。

What you could do is remove the global variables altogether, and always refer to the worksheet for the setting and retrieval of their values. 您可以做的是完全删除全局变量,并始终参考工作表以设置和获取它们的值。

You can improve your approach with a couple of helper routines 您可以通过一些辅助例程来改进方法

Dim gVars as Variant

Sub LoadVariables()
    ' Load data from s/s into variable
    gVars = Sheets("Main").Range(Cells(1, 1), Cells(3, 2)).Value
End sub

Sub SaveVariables()
    ' Save data from variable into variables
    Sheets("Main").Range(Cells(1, 1), Cells(3, 2)).Value = gVars
End sub

Then in your code use LoadVariables when you need to use the data (usually at the start of whatever sub needs it) and SaveVariables when you have updated the values. 然后在你的代码中使用LoadVariables当你需要使用的数据(通常是在任何的子需要它的开始)和SaveVariables当你更新的值。 Obviously this needs more error handling, etc. 显然,这需要更多的错误处理等。

The loading of the entire variables set into a single array also makes it easier to maintain the code (less chance of typos) and handle iterations if needed. 将整个变量集加载到单个数组中还可以更轻松地维护代码(减少错别字的机会)并在需要时处理迭代。

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

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