简体   繁体   English

Excel Form Combobox值到单元格

[英]Excel Form Combobox value to cell

I have an Excel VBA Form and ComboBox that has the 12 months of the year. 我有一个Excel VBA表单和ComboBox,一年中有12个月。 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. 我想要它做的是当我选择一个月时,它还应该在工作簿中的特定单元格中写入该月值,因为该单元格值在整个工作簿中链接。

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. 我的问题是,一旦打开工作簿加载,值不会刷新,也不会从ComboBox复制到单元格。 How can I make this work ? 我怎样才能做到这一点?

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

In Designmode you can double click the ComboBox to create the onChange method 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM