VBA 全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1174109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:37:13  来源:igfitidea点击:

VBA global variables

excelvbaglobal-variables

提问by stanigator

I have the following code for handling userforms on my spreadsheet VBA macros:

我有以下代码用于处理电子表格 VBA 宏上的用户表单:

Option Explicit
Public saved_vocab As String
Public saved_num As String
Public saved_def As String
Public saved_ex As String

Private Sub Save_Click()
    Dim low As Integer
    Dim high As Integer
    Dim selected As Integer

    low = 1
    high = Cells(1, 1).End(xlDown).Row
    Range(Cells(low, 1), Cells(high, 1)).Find(what:=vocab.Text, After:=ActiveCell, LookIn:=xlFormulas, _
          LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False, SearchFormat:=False).Activate
    selected = ActiveCell.Row

    saved_vocb = Cells(selected, 1).Text
    saved_num = Cells(selected, 2).Text
    saved_def = Cells(selected, 3).Text
    saved_ex = Cells(selected, 4).Text
    Cells(selected, 1) = vocab.Text
    Cells(selected, 2) = num.Text
    Cells(selected, 3) = definition.Text
    Cells(selected, 4) = example.Text
End Sub

Private Sub undo_Click()
    Cells(selected, 1) = saved_vocab
    Cells(selected, 2) = saved_num
    Cells(selected, 3) = saved_def
    Cells(selected, 4) = saved_ex
End Sub

What I want to do is save the information properly in saved_??? string values so that I can access it properly when I decide to click the "Undo" button to execute the second function. However, when I execute the second function, information previously saved in saved_??? variables are gone perhaps b/c of myself handling variable scopes improperly. What would be the easiest way to accomplish what I want to do? Thanks in advance for the advices.

我想要做的是将信息正确保存在saved_??? 字符串值,以便在我决定单击“撤消”按钮执行第二个函数时可以正确访问它。但是,当我执行第二个函数时,之前保存在saved_???中的信息?变量消失了,也许是我自己不正确地处理变量范围的 b/c。完成我想做的事情的最简单方法是什么?预先感谢您的建议。

回答by Jose Basilio

At a glance, I can see that the variable selectedis declared inside the Save_Click()Subroutine and it is used in the undo_Click()subroutine.

一看就知道变量selected是在Save_Click()子程序中声明的,在子程序中使用undo_Click()

The variable selectedneeds to be declared outside the subroutines along with your other global variables such as saved_vocab, saved_num, etc.

变量selected需要子程序外面与其他全局变量,例如一起宣布saved_vocabsaved_num等等。

Option Explicit
Public saved_vocab As String
Public saved_num As String
Public saved_def As String
Public saved_ex As String
Public selected As Integer 'declaration added

Then remove the existing declarion for selectedinside Save_Click()

然后删除现有的selected内部声明Save_Click()

回答by Lance Roberts

I think you want to put the Global Variables in a 'normal' Module, not a Sheet Module.

我认为您想将全局变量放在“普通”模块中,而不是工作表模块中。