vba Excel 表单组合框值到单元格

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

Excel Form Combobox value to cell

excelvbaexcel-vbacombobox

提问by CCM

I have an Excel VBA Form and ComboBox that has the 12 months of the year. What I want it to do is when I select a month, it should also write that month value in a specific cell in the workbook as that cell value is linked throughout the workbook.

我有一个 Excel VBA 表单和一年中有 12 个月的 ComboBox。我想要它做的是当我选择一个月份时,它还应该在工作簿的特定单元格中写入该月份的值,因为该单元格值在整个工作簿中都有链接。

My issue here is that once loaded on opening the workbook, the values do not refresh and they do not copy from the ComboBox to the cell. How can I make this work ?

我的问题是,一旦在打开工作簿时加载,这些值就不会刷新,也不会从 ComboBox 复制到单元格。我怎样才能使这项工作?

Private Sub Workbook_Open()

    With Form1
        .Show vbModeless 
        .ComboBox1.List = ThisWorkbook.Sheets("Liste").Range("D2:D13").Value 'month lists
    End With
    ThisWorkbook.Sheets("Sheet2").Range("B2").Value = Form1.ComboBox1.Value
End Sub

回答by Tim Schmidt

In Designmode you can double click the ComboBox to create the onChangemethod of your ComboBox.

在 Designmode 中,您可以双击 ComboBox 来创建 ComboBox 的onChange方法。

This method gets called every time, the value of your ComboBox is changed.

每次都会调用此方法,更改 ComboBox 的值。

In There you can put the code to write the value into the desired cell.

在那里,您可以将代码写入所需的单元格中。

Private Sub ComboBox1_Change()
    ThisWorkbook.Worksheets("Sheet2").Range("B2").Value = Form1.ComboBox1.Value
End Sub