2

我有以下代码用于处理我的电子表格 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

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

4

2 回答 2

4

一目了然,我可以看到该变量selected是在子例程中声明的Save_Click(),并且在undo_Click()子例程中使用。

该变量selected需要在子例程之外与您的其他全局变量(例如saved_vocab,saved_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

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

于 2009-07-23T20:05:48.863 回答
3

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

于 2009-07-23T19:58:10.360 回答