vba 在单元格中获取 ComboBox 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4145680/
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
Getting ComboBox Value In A Cell?
提问by sooprise
I have a named comboBox, let's call it: "comboBox1"
我有一个命名的组合框,我们称之为:“组合框1”
I want to reference the value of comboBox1 from a cell.
我想从单元格中引用 comboBox1 的值。
=if(comboBox1.Value=1,1,0)
The idea above is what I'm looking for. I know I can attach an even to comboBox1, which populates a cell, which can be read by other cells, but that just introduces more moving parts and complexity.
上面的想法是我正在寻找的。我知道我可以将偶数附加到comboBox1,它填充一个单元格,其他单元格可以读取它,但这只会引入更多的移动部分和复杂性。
This has to be possible, right? Any help would be great, thanks!
这一定是可能的,对吧?任何帮助都会很棒,谢谢!
采纳答案by Fionnuala
Excel allows a cell link on both an ActiveX and a forms doropdown (combo). This will write the value to a cell without any code.
Excel 允许 ActiveX 和表单 doropdown(组合)上的单元格链接。这会将值写入单元格而无需任何代码。
回答by Fink
I think something like this is possible.
我认为这样的事情是可能的。
For your combobox change event, you will need to trigger a recalculation:
对于组合框更改事件,您需要触发重新计算:
Private Sub ComboBox1_Change()
Application.Calculate
End Sub
Next, you will need to add a custom user defined function. The important piece of this the Application.Volatile line. This will make sure its recalculated, after any calculation.
接下来,您需要添加一个自定义的用户定义函数。Application.Volatile 行的重要部分。这将确保在任何计算后重新计算。
Function GetComboVal(cmbName As String) As String
Application.Volatile 'will always recalculate
Dim cmb As OLEObject
Set cmb = Sheet1.OLEObjects(cmbName)
GetComboVal = cmb.Object.Value
End Function
So in your cell, you will need to use a call like this:
因此,在您的单元格中,您需要使用如下调用:
=if(GetComboVal("ComboBox1")=1,1,0)
The Downside to this technique is that if your worksheet has many calculations, it could take a while to recalculate.
这种技术的缺点是,如果您的工作表有很多计算,则重新计算可能需要一段时间。
回答by AJ Geringer
Good Afternoon,
下午好,
There is a much easier way to link a cell to a combo-box. With-in the properties of the ComboBox, above ListFillRange is Linked Cell. You would just designate this cell to whatever you want your combobox value to equal too.
有一种更简单的方法可以将单元格链接到组合框。在 ComboBox 的属性中,ListFillRange 上方是 Linked Cell。您只需将此单元格指定为您希望组合框值也相等的任何值。

