VBA - 获取组合框的旧值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18085673/
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 22:35:23  来源:igfitidea点击:

VBA - Get OLD Value of Combobox

vbaexcel-vbacomboboxexcel

提问by darkZone

I would like to get the old value of a ComboBox when the combobox value will change.

当组合框值发生变化时,我想获得组合框的旧值。

I have tried something like:

我尝试过类似的事情:

Private Sub ComboBox1_Change()
        Application.EnableEvents = False
        newVal = ComboBox1.Value
        Application.Undo
        oldVal = ComboBox1.Valu
End Sub

or

或者

Private Sub ComboBox1_Change()
        Application.EnableEvents = False
        newVal = ComboBox1.Value
        ComboBox1.Undo
        oldVal = ComboBox1.Valu
End Sub

but it seems not to work...

但它似乎不起作用......

Thanks

谢谢

回答by Doug Glancy

You could use a Static variablethat holds its value between calls to the ComboBox1_Changeevent:

你可以使用一个静态变量来保存它在ComboBox1_Change事件调用之间的值:

Private Sub ComboBox1_Change()
Static OldValue As String

With Me.ComboBox1
    Debug.Print "Value: "; .Value; " Old Value: "; OldValue
    OldValue = .Value
End With
End Sub

If you need to access OldValue outside of the Change event, use a module-level variable, as described by @Ifrandom.

如果您需要在 Change 事件之外访问 OldValue,请使用模块级变量,如@Ifrandom 所述。

回答by zin

I use excel combo boxes alot, and have developed a number of useful features like: * save & load combo box data from the registry or a hidden "APP_DATA" worksheet * add a new permanent combo box item by entering a new value and pressing * allowing editing of combo history by double-clicking on the box * clearing all combo history by erasing any currently showing item and pressing

我经常使用 excel 组合框,并开发了许多有用的功能,例如: * 从注册表或隐藏的“APP_DATA”工作表中保存和加载组合框数据 * 通过输入新值并按 * 添加新的永久组合框项目允许通过双击框编辑组合历史 * 通过擦除任何当前显示的项目并按下来清除所有组合历史

These are just some ideas to get you going, the code is fairly simple, I just wrote some simple subs for: * load a combo box from a history string * dedup a delimited string * event to trap a for "new item" or "erase items" function * event to trap a for "edit items" function

这些只是一些让你开始的想法,代码相当简单,我只是写了一些简单的子: * 从历史字符串加载组合框 * 删除一个分隔的字符串 * 事件以捕获“新项目”或“擦除项目”功能 * 事件以捕获“编辑项目”功能

When a new item is added, I just append it to the history string, and dedup it just in case. History strings are saved or loaded from registry on initialize and terminate, or as they are changed, and initialize also populates the combos. I always assumed there would be an easy way to do this, since I see so many combo boxes maintaining history (I limit to the latest 24 items), but I never found any code, so I just made my own. In some apps, even double-clicking a worksheet cell value can populate or CSV-append to a combo box, or a command button can prompt for and load a series of cells into a CSV list into combo, very useful.

当添加一个新项目时,我只是将它附加到历史字符串中,以防万一。历史字符串在初始化和终止时保存或从注册表加载,或者在它们更改时,初始化也会填充组合。我一直认为有一种简单的方法可以做到这一点,因为我看到了很多维护历史记录的组合框(我限制为最新的 24 个项目),但我从未找到任何代码,所以我只是制作了自己的代码。在某些应用程序中,即使双击工作表单元格值也可以填充或 CSV 附加到组合框,或者命令按钮可以提示输入一系列单元格并将其加载到 CSV 列表中,非常有用。

回答by lfrandom

This is a bit more work, but what I've done in the past is create a private variable which contains the value.

这是更多的工作,但我过去所做的是创建一个包含该值的私有变量。

Basically on the change event, you do what you need to do with the old value, then you update the old value variable to the new value.

基本上在更改事件中,您对旧值执行您需要执行的操作,然后将旧值变量更新为新值。

Private oldValueComboBox as String

Private Sub ComboBox1_Change()
    ' Do whatever you need to do with the old value, in this case msgbox
    msgbox oldValueComboBox

    ' Set the old value variable to the new value
    oldValueComboBox = ComboBox1
End Sub

You can also use a static variable as another post mentions. If you use a static variable, it is only usable within the scope of the combobox change, if you use a private variable it is visible to the entire form.

您还可以使用静态变量作为另一篇文章的提及。如果您使用静态变量,则它只能在组合框更改的范围内使用,如果您使用私有变量,则它对整个表单可见。