我有以下代码用于处理电子表格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
我想要做的是在保存的_ ???中正确保存信息当我决定单击“撤消”按钮执行第二个函数时,我可以正确访问它。但是,当我执行第二个函数时,先前保存的信息保存在_ ???变量可能不在我自己处理变量范围不正确的b / c。什么是最简单的方法来完成我想做的事情?提前感谢您的建议。
答案 0 :(得分: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()
的现有声明
答案 1 :(得分:3)
我认为您希望将全局变量放在“普通”模块中,而不是表单模块中。